【问题标题】:c#/unity json error,and want to retrieve jsonObject within a JsonObject with Unity JsonUtility or litJsonc#/unity json 错误,并希望使用 Unity JsonUtility 或 litJson 在 JsonObject 中检索 jsonObject
【发布时间】:2021-05-01 01:30:32
【问题描述】:

所以我的问题是双重的,首先我有我不理解的行为,这是由于我对 Json 的一些误解。因此,鉴于下面的第一个示例,我得到了我想要的。

{
    "data":{
        "stats":{
            "kills":0,
            "gamesPlayed":30,
            "shotsFired":30
        }
    },
    "other":1
}

代码:

JsonData jsonData = JsonMapper.ToObject (jsonString);
JsonData entries = jsonData["data"]["stats"];
m_kills = int.Parse(entries["kills"].ToString());
Debug.log(m_kills);

输出

“0”

但是,当我在下一个 json 的代码后面使用相同的逻辑时,我得到一个错误。 那么为什么在使用下面的Json和代码的时候会这样

{
    "data":{
        "otherInfo":2,
        "scoreboard":[
            {
                "player":"bob",
                "score":57,
                "pdata":{
                    "a":40,
                    "b":20
                }
            },
            {
                "player":"joe",
                "score":32,
                "pdata":{
                    "a":20,
                    "b":13
                }
            }
        ],
        "otherInfo2":5
    }
}

代码

JsonData jsonData = JsonMapper.ToObject (jsonString);
JsonData entries = jsonData["data"];
m_info = int.Parse(entries["otherinfo"].ToString());
Debug.log(m_info);

我明白了

KeyNotFoundException:给定的键不在字典中。 System.Collections.Generic.Dictionary`2[TKey,TValue].get_Item (TKey key) (at :0)

里面有什么问题?

JsonData entries = jsonData["data"];
m_info = int.Parse(entries["otherinfo"].ToString());

至于第二部分,第一部分只是为了更好地理解它。我的最终目标是获得每个玩家的得分(除了 bob 和 joe 希望它可以扩展),然后还获取 pdata ......所以猜这个问题是 3 倍,但对这 3 个中的任何一个的帮助将不胜感激!

谢谢

【问题讨论】:

    标签: c# arrays json unity3d


    【解决方案1】:

    你应该为你的 json 创建一个数据模型。之后,您可以将您的 json 字符串转换为有效的对象以达到您的必要属性。

    // the data models based on your json string
    public class Pdata    {
        public int a { get; set; } 
        public int b { get; set; } 
    }
    
    public class Scoreboard    {
        public string player { get; set; } 
        public int score { get; set; } 
        public Pdata pdata { get; set; } 
    }
    
    public class Data    {
        public int otherInfo { get; set; } 
        public List<Scoreboard> scoreboard { get; set; } 
        public int otherInfo2 { get; set; } 
    }
    
    public class Root    {
        public Data data { get; set; } 
    }
    
    // code 
    Root root = JsonConvert.DeserializeObject<Root>(yourJsonString); 
    int otherInfo = root.data.otherInfo;
    

    您可以在https://json2csharp.com/ 轻松创建模型

    【讨论】:

      【解决方案2】:

      我也花了一段时间才发现,但您的错误似乎是错字!

      在 json 中它是 "otherInfo":2,但您正在尝试访问 entries["otherinfo"]


      一般而言,您应该在c# 中实现相应的数据结构(json2csharp 有帮助)

      [Serializable]
      public class Pdata
      {
          public int a; 
          public int b; 
      }
      
      [Serializable]
      public class Player
      {
          public string player; 
          public int score; 
          public Pdata pdata; 
      }
      
      [Serializable]
      public class Data
      {
          public int otherInfo; 
          public List<Player> scoreboard; 
          public int otherInfo2; 
      }
      
      [Serializable]
      public class Root
      {
          public Data data; 
      }
      

      然后使用JsonUtility.FromJson

      var root = JsonUtility.FromJson<Root>(jsonString);
      
      Debug.Log($"otherInfo: {root.data.otherInfo}");
      
      foreach(var player in root.data.scoreboard)
      {
          Debug.Log($"Name: {player.player}, Score: {player.score}, A: {player.pdata.a}");
      }
      

      对于您当前的数据结构,我不建议您使用第三方库,JsonUtility 在这里很好。

      但是,Unity 内置 JsonUtility 的反序列化非常有限(参见 Unity - Script Serialization)。

      如果它变得更复杂并且您想使用例如将来的字典,那么您将不得不使用其他东西,例如Newtonsoft .NET JSON.

      那就是

      var root = JsonConvert.DeserializeObject<Root>(jsonString); 
      ...
      

      【讨论】:

        【解决方案3】:

        您的 JSON 开头缺少“{”,结尾缺少“}”。

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2017-07-26
          • 2015-01-27
          • 1970-01-01
          相关资源
          最近更新 更多