【问题标题】:How do I edit JSON values in c# with LitJson in Unity3D?如何在 Unity3D 中使用 LitJson 在 c# 中编辑 JSON 值?
【发布时间】:2017-02-10 17:12:07
【问题描述】:

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

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

这是我的 .json 脚本:

[
    {
    "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);
    }
}

此脚本由于某种原因无法运行。我不断收到此错误:

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)

【问题讨论】:

    标签: c# json unity3d


    【解决方案1】:

    是的,这就是这些库的目的。我对 LitJson 并不特别熟悉,但其中大部分是相似的:

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

    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:

    {
        ""Name""     : ""Thomas More"",
        ""Age""      : 23,
        ""Birthday"" : ""02/07/1478 00:00:00""
    }
    

    如果您想在一个 json 中包含多个 Person,您可以将它们添加到一个列表中并序列化该列表。如果您想要每个不同对象的唯一标识符,除其他外,您可以向 Person 添加一个 Id 属性。像这样:

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

    public string Id { get; set; }
    

    以及用法/列表序列化:

    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:

    {
        {
            ""Name""     : ""Thomas"",
            ""Id""       : ""0001""
        },
        {
            ""Name""     : ""Albert"",
            ""Id""       : ""0002""
        }
    }
    

    【讨论】:

    • 因此,如果我需要多个相同类型的对象,那在我的脚本中是如何工作的?我需要做一个清单,所以我需要访问多个对象中的一个。
    • 你列出了这些对象。 List objList = new List();
    • 但是我怎样才能使它只有一个对象进入列表的每个元素?基本上我如何分离元素。
    • 找到您要查找的对象并编辑其属性。 people.Where(x =&gt; x.Name == "Albert").First().Name = "Albert Einstein"; 并再次保存。 JsonMapper.ToJson(people) 并且,我想,覆盖旧文件。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-12-27
    • 1970-01-01
    • 1970-01-01
    • 2012-11-08
    • 2019-05-21
    • 2011-06-24
    相关资源
    最近更新 更多