C# Unity asset Newtonsoft Json从文件读取和写入多条记录?

C# Unity asset Newtonsoft Json从文件读取和写入多条记录?,c#,unity3d,json.net,C#,Unity3d,Json.net,在json文件中读取和写入多条记录时遇到问题。在我添加“Items”数组并添加第二条记录之前,它很好地加载了文件。我的问题是,我试图加载多个项目的球员库存。当我尝试这样做时,它只加载一个项目,并说“阅读完JSON内容后遇到的其他文本”,它指向JSON文件中的行号,该行号结束第一个项目。另外,当尝试使用for循环保存时,它会给我一个错误,指出属性名称已经存在,因此我尝试将整个内容设置为数组,但这也不起作用。 我还想指出,这个版本的Json与我见过的其他一些版本不同,因此很难找到关于这个的信息。我使

在json文件中读取和写入多条记录时遇到问题。在我添加“Items”数组并添加第二条记录之前,它很好地加载了文件。我的问题是,我试图加载多个项目的球员库存。当我尝试这样做时,它只加载一个项目,并说“阅读完JSON内容后遇到的其他文本”,它指向JSON文件中的行号,该行号结束第一个项目。另外,当尝试使用for循环保存时,它会给我一个错误,指出属性名称已经存在,因此我尝试将整个内容设置为数组,但这也不起作用。 我还想指出,这个版本的Json与我见过的其他一些版本不同,因此很难找到关于这个的信息。我使用的是Unity资产存储中的Json。 任何帮助都将不胜感激

{
  "Items":
   [
    {
      "Id": 0,
      "Title": "Test0",
      "Value": 15,
      "Description": "TESTing0",
      "Stackable": true,
      "Rarity": 1,
      "Slug": "TEST0",
      "Stats": 
     [ 
           {
            "Power": 9,
            "Defense" : 7,
            "Vitality": 3
           }
     ],

     "Id": 1,
      "Title": "Test1",
      "Value": 16,
      "Description": "TESTing1",
      "Stackable": false,
      "Rarity": 1,
      "Slug": "TEST1",
      "Stats": 
      [
            {
            "Power": 9,
            "Defense" : 7,
            "Vitality": 3
            }
      ]
    }
  ]
}
这是到目前为止我的代码。 我的保存和加载功能完全不同,因为我一直在尝试不同的方法,以使其按预期方式工作。现在我的Load函数正在尝试加载“Items”数组:实际上是多条记录。由于某些原因,这对我不起作用,我收到一条错误消息,说“访问的JObject值的键值无效:0。应为对象属性名。” 我也尝试过在没有数组“Items”的情况下这样做:但是它读取了一条记录,并说“在读取完JSON内容后遇到了额外的文本:”

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
using System.IO;

public class PlayerDatabase : MonoBehaviour
{
    private List<Item> database = new List<Item>();

    public class Item
    {
        public int Id { set; get; }
        public string Title { set; get; }
        public int Value { set; get; }
        public int Power { set; get; }
        public int Defense { set; get; }
        public int Vitality { set; get; }
        public string Description { set; get; }
        public bool Stackable { set; get; }
        public int Rarity { set; get; }
        public string Slug { set; get; }

        public Item(int id, string title, int value, int power, int defense, int vitality, string description, bool stackable, int rarity, string slug)
        {
            this.Id = id;
            this.Title = title;
            this.Value = value;
            this.Power = power;
            this.Defense = defense;
            this.Vitality = vitality;
            this.Description = description;
            this.Stackable = stackable;
            this.Rarity = rarity;
            this.Slug = slug;
        }
    }

    void Save()
    {
        JObject playerJson = new JObject();

        for (int i = 0; i < 1; i++)
        {
            playerJson.Add("Id", i);
            playerJson.Add("Title", Title);
            playerJson.Add("Value", Value);
            playerJson.Add("Description", Description);
            playerJson.Add("Stackable", Stackable);
            playerJson.Add("Rarity", Rarity);
            playerJson.Add("Slug", Slug);

            JArray jaStats = new JArray();
            jaStats.Add(Power);
            jaStats.Add(Defense);
            jaStats.Add(Vitality);
            playerJson.Add("Stats", jaStats);

            string path = Application.dataPath + "/Inventory System/StreamingAssets/Player.Json";
            File.WriteAllText(path, playerJson.ToString());
        }   
    }


    void Load()
    {
        string path = Application.dataPath + "/Inventory System/StreamingAssets/Player.Json";
        string jsonString = File.ReadAllText(path);
        JObject itemJson = JObject.Parse(jsonString);

        //Load values
        for (int i = 0; i < itemJson.Count; i++)
        {

            database.Add(new Item((int)itemJson[i]["Items"]["Id"], (string)itemJson[i]["Items"]["Title"], (int)itemJson[i]["Items"]["Value"],
            (int)itemJson[i]["Items"]["Stats"]["Power"], (int)itemJson[i]["Items"]["Stats"]["Defense"], (int)itemJson[i]["Items"]["Stats"]["Vitality"],
            (string)itemJson[i]["Items"]["Description"], (bool)itemJson[i]["Items"]["Stackable"], (int)itemJson[i]["Items"]["Rarity"], (string)itemJson[i]["Items"]["Slug"]));
        }
        Debug.Log(database[0].Power);
    }

    // Update is called once per frame
    void Update ()
    {
        if (Input.GetKeyDown(KeyCode.S)) Save();
        if (Input.GetKeyDown(KeyCode.L)) Load();
    }
}
使用系统集合;
使用System.Collections.Generic;
使用UnityEngine;
使用Newtonsoft.Json;
使用Newtonsoft.Json.Linq;
使用System.IO;
公共类玩家数据库:MonoBehavior
{
私有列表数据库=新列表();
公共类项目
{
公共int Id{set;get;}
公共字符串标题{set;get;}
公共int值{set;get;}
公共整数幂{set;get;}
公共整数防御{set;get;}
公共整数{set;get;}
公共字符串说明{set;get;}
公共布尔可堆叠{set;get;}
公共整数稀有性{set;get;}
公共字符串Slug{set;get;}
公共物品(int-id、字符串标题、int-value、int-power、int-defence、int-vitality、字符串描述、bool-stackable、int-rarity、string-slug)
{
这个.Id=Id;
这个.Title=Title;
这个。值=值;
这个。力量=力量;
这.防御=防御;
这.活力=活力,;
这个。描述=描述;
this.Stackable=可堆叠;
这个。稀有=稀有;
这个.Slug=Slug;
}
}
作废保存()
{
JObject playerJson=new JObject();
对于(int i=0;i<1;i++)
{
playerJson.Add(“Id”,i);
playerJson.Add(“Title”,Title);
playerJson.Add(“Value”,Value);
playerJson.Add(“Description”,Description);
playerJson.Add(“Stackable”,Stackable);
playerJson.Add(“稀有”,稀有);
Add(“Slug”,Slug);
JArray jaStats=新的JArray();
jaStats.Add(电源);
添加(防御);
添加(活力);
添加(“Stats”,jaStats);
字符串路径=Application.dataPath+“/Inventory System/StreamingAssets/Player.Json”;
File.WriteAllText(路径,playerJson.ToString());
}   
}
空荷载()
{
字符串路径=Application.dataPath+“/Inventory System/StreamingAssets/Player.Json”;
字符串jsonString=File.ReadAllText(路径);
JObject itemJson=JObject.Parse(jsonString);
//荷载值
for(int i=0;i
“多个”是指
项目中的多个“项目”,或多个
项目
s?我创建的项目类型列表(数据库)应在其中存储多个项目。这是json文件“Id”:0,“Title”:“Test0”,“Value”:15,“Description”:“TESTing0”,“Stackable”:true,“Rarity”:1,“Slug”:“Test0”,“Stats”:[{“Power”:9,“defence”:7,“Vitality”:3}]你的json结构把我弄糊涂了。逐一地。首先,您现在显示的json不是
{Items:[{id:0,…},{id:0,…},…}}
,而是
{Items:[{id:0,…,id:0,…}]}
。这是正确的吗?我正要写一个答案,但我认为最好标记为重复,因为Unity的
JsonUtility
可以处理这个json。此外,我还回答了如何保存和加载json数据,这正是您正在做的。我没必要重复这些。副本中有两个链接。如果你仍然有问题,让我知道我似乎已经开始工作了,但是当它到达Items数组中的Stats数组时,它说参数超出了范围。这是我重新格式化的json文件,您可以更好地理解它。{“项目”:[{“Id”:1,“标题”:“Test0”,“Value”:15,“描述”:“TESTing0”,“Stackable”:true,“稀有”:1,“Slug”:“Test0”,“Stats”:[{“Power”:9,“defence”:7,“Vitality”:3}]},{“Id”:2,“Title”:“Test1”