如何在Unity3D中使用LitJson在c#中编辑JSON值? [英] How do I edit JSON values in c# with LitJson in Unity3D?
问题描述
因此,我尝试获取.json文件并用c#读取它,然后修改其中一个值,然后刷新.json文件,以便更改值.我知道如何读取文件以及如何覆盖文件,但是如何更改文件中一项特定项的一项特定值?
So I am trying to take a .json file and read it in c#, then modify one of the values, and then refresh the .json file so that the value is changed. I know how to read the file, and how to overwrite the file, but how do I change one specific value of one specific item in the file?
此脚本的目标是创建一个库存系统,可以在其中通过json文件记录余额.我想使用BalanceChange()函数转到文件,使用ID变量找到正确的项目,然后相应地更改余额.
The goal of this script is to create an inventory system, where the balance can be recorded through the json file. I want to use the BalanceChange() function to go to the file, find the correct item using the ID variable, and then change the balance accordingly.
这是我的.json脚本:
Here is my .json script:
[
{
"name":"Potion",
"id":1,
"balance":5
},
{
"name":"Neutral Bomb",
"id":2,
"balance": 4
}
]
这是我的c#脚本:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using System.IO;
using LitJson;
public class Item
{
public string Name { get; set; }
public int Id { get; set; }
public int Balance { get; set; }
public Item(string name1, int id1, int balance1) {
Name = name1;
Id = id1;
Balance = balance1;
}
}
public class InventoryAttempt : MonoBehaviour
{
public static void BalanceChange(string filePath, int ID, int change)
{
string jsonString = File.ReadAllText(Application.dataPath + filePath);
List<Item> itemList = JsonMapper.ToObject<List<Item>>(jsonString);
itemList [ID].Balance += change;
jsonString = JsonMapper.ToJson (itemList).ToString();
File.WriteAllText (Application.dataPath + filePath, jsonString);
}
void Start()
{
BalanceChange ("/Scripts/inventory.json", 1, -1);
}
}
由于某些原因,该脚本无法正常工作.我不断收到此错误:
This script isn't working for some reason. I keep getting this error:
MissingMethodException: Method not found: 'Default constructor not found...ctor() of Item'.
System.Activator.CreateInstance (System.Type type, Boolean nonPublic) (at /Users/builduser/buildslave/mono/build/mcs/class/corlib/System/Activator.cs:368)
System.Activator.CreateInstance (System.Type type) (at /Users/builduser/buildslave/mono/build/mcs/class/corlib/System/Activator.cs:254)
LitJson.JsonMapper.ReadValue (System.Type inst_type, LitJson.JsonReader reader)
LitJson.JsonMapper.ReadValue (System.Type inst_type, LitJson.JsonReader reader)
LitJson.JsonMapper.ToObject[List`1] (System.String json)
InventoryAttempt.BalanceChange (System.String filePath, Int32 ID, Int32 change) (at Assets/Scripts/InventoryAttempt.cs:26)
InventoryAttempt.Start () (at Assets/Scripts/InventoryAttempt.cs:34)
推荐答案
是的,这就是这些库的目的.我对LitJson并不特别熟悉,但是其中大多数是相似的:
Yes, that's the purpose of these librarys. I'm not familiar with LitJson specifically but most of these are similar:
基于疑问示例;我们将json转换为对象,更改属性,然后再次将其保存到json:
Based on the doucmentation examples; we take json and turn it to an object, change a property and then save it to json again:
public class Person
{
// C# 3.0 auto-implemented properties
public string Name { get; set; }
public int Age { get; set; }
public DateTime Birthday { get; set; }
}
public class Example{
public static void JsonToPerson()
{
string json = @"
{
""Name"" : ""Thomas More"",
""Age"" : 57,
""Birthday"" : ""02/07/1478 00:00:00""
}";
Person thomas = JsonMapper.ToObject<Person>(json);
thomas.Age = 23;
string newJson = JsonMapper.ToJson(thomas);
}
}
要编辑属性,我们只需更改Objects属性.在我的示例中,我将thomas.Age编辑为23,然后再次将该对象序列化为json. 应该给我们下面的json:
To edit a property we simply change the Objects property. In my example I edit thomas.Age to 23 and then serialize the object to json again. Should give us the following json:
{
""Name"" : ""Thomas More"",
""Age"" : 23,
""Birthday"" : ""02/07/1478 00:00:00""
}
如果您想在一个json中包含多个Person,请将它们添加到列表中并序列化该列表.如果您想要每个不同对象的唯一标识符,则可以在Person中添加一个Id属性.像这样:
If you'd like to have many Person in one json, you add them to a list and serialize the list. If you'd like an unique identifier for every distinct object you could, among other things, add an Id property to Person. Like this:
首先将ID属性添加到Person类:
First add the Id propery to the Person class:
public string Id { get; set; }
以及用法/列表序列化:
and the usage/list serialization:
List<Person> people = List<Person>();
Person thomas = new Person();
thomas.Name = "Thomas";
thomas.Id = "0001";
people.Add(thomas);
Person albert = new Person();
albert.Name = "Albert";
albert.Id = "0002";
people.Add(albert);
JsonMapper.ToJson(people);
这应该给你Json这样的东西:
This should give you Json like this:
{
{
""Name"" : ""Thomas"",
""Id"" : ""0001""
},
{
""Name"" : ""Albert"",
""Id"" : ""0002""
}
}
这篇关于如何在Unity3D中使用LitJson在c#中编辑JSON值?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!