【问题标题】:How to deserialize a JSON Properties containing numbers using POCO class?如何使用 POCO 类反序列化包含数字的 JSON 属性?
【发布时间】:2018-12-26 13:43:00
【问题描述】:

我在 Unity 引擎中使用 JSONUtility 练习 JSON 反序列化。当属性为字符串时,我在 stackoverflow(Link) 的帮助下使用 POCO 类解决了一些示例。我使用确切名称解决了以下 JSON.Link在 POCO 类中。但是当 JSON 属性包含如下整数时。

{


"1": {
    "Armor": 1, 
    "Strenght": 1
  }, 
  "0": {
    "Armor": 1, 
    "Mana": 2, 
    "Strenght": 1, 
    "Health": 1, 
    "Power": 1
  }
}

以类似的方式解决没有成功。根据我得到的反馈,我必须使用字典。

 IEnumerator GetExampleValues()
{
    string URLss = "http://www.json-generator.com/api/json/get/bOQGnptixu?indent=2";
    WWW readjson = new WWW(URLss);
    yield return readjson;

    Debug.Log("ReadJSON"+readjson.text);


}

下面是我的 POCO 课

using System.Collections.Generic;

[System.Serializable]
public class Exampleget  
{
    public int Power;
    public int Mana;
    public int Strenght;
    public int Armor;
    public int Health;

}

public class GetNumbers
{

    public List<Exampleget> onevalues;
}

【问题讨论】:

    标签: c# json unity3d poco


    【解决方案1】:

    解决此问题的一种方法是使用 JsonExtensionData 属性

    public class Exampleget  
    {
        public int Power;
        public int Mana;
        public int Strenght;
        public int Armor;
        public int Health;
    
    }
    
    public class GetNumbers
    {
        [JsonExtensionData]
        public Dictionary<string,dynamic> Values;
    }
    

    有了类,现在你可以了,

    var str = @"{
            '1': {
                'Armor': 1, 
                'Strenght': 1
              }, 
              '0': {
                'Armor': 1, 
                'Mana': 2, 
                'Strenght': 1, 
                'Health': 1, 
                'Power': 1
              }
            }";
    
    var result = JsonConvert.DeserializeObject<GetNumbers>(str);
    foreach(var item in result.Values)
    {
        Console.WriteLine($"For Key {item.Key},Power = {item.Value.Power}, Mana = {item.Value.Mana}, Armor = {item.Value.Armor}, Health = {item.Value.Health}");
    }
    

    输出

    For Key 1,Power = , Mana = , Armor = 1, Health = 
    For Key 0,Power = 1, Mana = 2, Armor = 1, Health = 1
    

    更新

    如果您想将 Dictionary 设为Dictionary&lt;string,Exampleget&gt; 而不是使用动态,那么您可以使用一个附加属性来转换为 Exampleget。但这是以额外的序列化/反序列化为代价的,因此除非您有充分的理由坚持下去,否则不建议这样做。

    public class GetNumbers
    {
        [JsonExtensionData]
        public Dictionary<string,object> Values;
        [JsonIgnore]
        public Dictionary<string,Exampleget> ExamplegetDictionary=> Values.Select(x=>new KeyValuePair<string,Exampleget>(x.Key, ((object)x.Value).Cast<Exampleget>()))
                                                                          .ToDictionary(x=>x.Key,y=>y.Value);
    
    }
    
    public static class Extension
    {
        public static T Cast<T>(this object value)
        {
            var jsonData = JsonConvert.SerializeObject(value);
            return JsonConvert.DeserializeObject<T>(jsonData);
        }
    }
    

    【讨论】:

    • 我们需要添加任何插件..我使用 JsonUtility 进行统一。我试图使用 Jsonutility 解决。什么是'T'Cast ..我不明白...请查......stackoverflow.com/questions/53882500/…
    • 上面代码Newtonsoft.Json进行反序列化。 Cast 方法用于将动态对象强制转换为 T(在本例中为 Exampleget)。
    猜你喜欢
    • 2015-07-29
    • 1970-01-01
    • 1970-01-01
    • 2020-09-02
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多