首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何使用Unity3D中的LitJson编辑c#中的JSON值?

如何使用Unity3D中的LitJson编辑c#中的JSON值?
EN

Stack Overflow用户
提问于 2017-02-11 01:12:07
回答 1查看 1.7K关注 0票数 1

因此,我尝试获取一个.json文件并在c#中读取它,然后修改其中一个值,然后刷新.json文件以便更改该值。我知道如何读取文件,以及如何覆盖文件,但是如何更改文件中某个特定项的特定值?

此脚本的目标是创建一个库存系统,其中可以通过json文件记录余额。我想使用BalanceChange()函数转到该文件,使用ID变量找到正确的项目,然后相应地更改余额。

下面是我的.json脚本:

代码语言:javascript
复制
[
    {
    "name":"Potion",
    "id":1,
    "balance":5
    },
    {
    "name":"Neutral Bomb",
    "id":2,
    "balance": 4
    }
]

下面是我的c#脚本:

代码语言:javascript
复制
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);
    }
}

由于某些原因,此脚本无法工作。我一直收到这个错误:

代码语言:javascript
复制
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)
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2017-02-11 01:24:01

是的,这就是这些库的目的。我并不特别熟悉LitJson,但它们中的大多数都是相似的:

基于doucmentation示例;我们将json转换为对象,更改属性,然后再次将其保存为json:

代码语言:javascript
复制
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);
    }
}

要编辑属性,我们只需更改对象属性。在我的示例中,我将thomas.Age编辑为23,然后再次将对象序列化为json。应该会给我们提供以下json:

代码语言:javascript
复制
{
    ""Name""     : ""Thomas More"",
    ""Age""      : 23,
    ""Birthday"" : ""02/07/1478 00:00:00""
}

如果您希望在一个json中有多个人,可以将他们添加到一个列表中并序列化该列表。如果您希望每个不同的对象都有一个惟一的标识符,那么可以向Person添加一个Id属性。如下所示:

首先,将Id属性添加到Person类:

代码语言:javascript
复制
public string Id { get; set; }

和使用/列表序列化:

代码语言:javascript
复制
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:

代码语言:javascript
复制
{
    {
        ""Name""     : ""Thomas"",
        ""Id""       : ""0001""
    },
    {
        ""Name""     : ""Albert"",
        ""Id""       : ""0002""
    }
}
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/42165004

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档