如何使用Newton Soft解析嵌套对象的JSON [英] How to parse this JSON using Newton Soft for nested object

查看:808
本文介绍了如何使用Newton Soft解析嵌套对象的JSON的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何解析此JSON

使用Newton Soft

using Newton Soft

我尝试过,但给我空值,因为我的模态应该具有1、2、3类... 但这是动态的.所以要弄糊涂了.

I tried but gives me null as my modal should have class 1, 2, 3 ... but that's dynamic .So getting confuse.

感谢您的帮助!

{
"data": {
    "1": {
        "test": {
            "col1": "123",
            "col2": "name"
        }
    },
    "2": {
        "test": {
            "col1": "345",
            "col2": "name2"
        }
    },
    "3": {
        "test": {
            "col1": "456",
            "col2": "name3"
        }
    }
}

class root 
{ 
    data data{get; set;};
}

class data
{ 
    List<object> innerObject {get; set;} //not sure as labels are dynamic 
} 

class test
{ 
    col1{get; set;} 
    col2{get; set;} 
} //Calling it in that way .. 

root r = JsonConvert.DeserializeObject<root>(result);

推荐答案

您可以使用字典来解析具有自定义属性名称的JSON对象:

You can use a dictionary to parse a JSON object with custom property names:

public class Test
{
    public string col1 { get; set; }
    public string col2 { get; set; }
}

public class DataValue
{
    public Test test { get; set; }
}

public class RootObject
{
    public RootObject() { this.data = new Dictionary<string, DataValue>(); }
    public Dictionary<string, DataValue> data { get; set; }
}

请参见反序列化字典.

如果您确定 词典键将始终为数字,则可以使用整数键,并使用

If you are sure the dictionary keys will always be numeric, you can use integer keys, and use a SortedDictionay to order the values:

public class RootObject
{
    public RootObject() { this.data = new SortedDictionary<long, DataValue>(); }
    public SortedDictionary<long, DataValue> data { get; set; }
}

这篇关于如何使用Newton Soft解析嵌套对象的JSON的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆