繁体   English   中英

Json反序列化词典 <key,value> 返回null

[英]Json deserialize Dictionary<key,value> return as null

快一点!

我有三个对象类。

public class Parent
{
    public string name { get; set; }
    public Children child{ get; set; }
}

public class Children
{
    public Dictionary<string, item> item{ get; set; }
}

public class item
{
    public string id { get; set; }
    public string type { get; set; }
}

我正在使用的JSON字符串:

        {
            'name': 'Child1',
            'Children': {
               'item-1253': {
                   'id': 'car',
                   'type': 'car'
               },
               'item-4343': {
                   'id': 'car',
                   'type': 'car'
               }......
           }
        }

用过的:

test = JsonConvert.DeserializeObject<Parent>(json);

输出为"item": null 我可以问为什么? 如何访问此项目对象中的所有属性,因为每个子项将有多个项目,项目对象名称是动态的,例如item_id12434 我想这个对象是独立的,我该如何实现呢? 会这样的吗? List<Dictionary<string, item>>

修改您的类看起来像这样,它将正确反序列化。

public static class Program
{
    static void Main(string[] args)
    {
        string str = @"{
                'name': 'Child1',
                'Children': {
                    'item-1253': {
                        'id': 'car',
                        'type': 'car'
                    },
                    'item-4343': {
                        'id': 'car',
                        'type': 'car'
                    }
                }
            }";

        // Way 1) Using POCO
        Parent parent = JsonConvert.DeserializeObject<Parent>(str);

        // Way 2) Using dynamic
            dynamic deserializedDynamicObject = JsonConvert.DeserializeObject<dynamic>(str);

        // Way 3) Using JObject
        JObject deserializedJObject = JsonConvert.DeserializeObject<JObject>(str);
        List<JObject> childrenObjects = deserializedJObject["Children"]
                                        .Cast<JProperty>()
                                        .Select(x => x.Value)
                                        .Cast<JObject>()
                                        .ToList();
    }
}

public class Parent
{
    [JsonProperty("name")]
    public string Name { get; set; }

    [JsonProperty("Children")]
    public Dictionary<string, Item> Children { get; set; }
}

public class Item
{
    [JsonProperty("id")]
    public string Id { get; set; }

    [JsonProperty("type")]
    public string Type { get; set; }
}

修复示例: https//dotnetfiddle.net/7TK008


请查看工作示例 - https://dotnetfiddle.net/eiMEkr

你提出了两个问题:

  1. 错误的参数名称儿童 - 应该是孩子

  2. 错误的字典对象定义

    const string json = @"
                {
                   'name':'Child1',
                   'child':{
                      'item':{
                         'dict_item':{
                            'id':'car1',
                            'type':'car2'
                         }
                      }
                   }
                }";

   var test = JsonConvert.DeserializeObject<Parent>(json);

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM