首页 新闻 会员 周边

c# Newtonsoft.Json JSON序列化 多层

0
悬赏园豆:160 [已解决问题] 解决于 2016-07-20 11:08

弄了一天了,没有弄出来,

打算用Newtonsoft.Json 序列化成对象,但对JSON数据格式不是很熟悉,导致序列化不出来,请指教下,谢谢!

字符串:

json10({
    "ResT": [
        [
            "",
            "15"
        ],
        
        [
            "",
            "65"
        ]
    ],
    "Mc": [
        {
            "index": "1",
            "type": "t_group",
            "data": [
                [
                    {
                        "t123": ""
                    },
                    {
                        "t123": ""
                    },
                    {
                        "t123": "",
                        "type": "o"
                    },
                    {
                        "t123": ""
                    }
                ],
                [
                    {
                        "t123": ""
                    },
                    {
                        "t123": "",
                        "type": "o"
                    },
                    {
                        "t123": ""
                    },
                    {
                        "t123": ""
                    },
                    {
                        "t123": ""
                    },
                    {
                        "t123": ""
                    }
                ]
            ]
        },
       
        {
            "index": "2",
            "type": "t_group",
            "data": [
                [
                    {
                        "t123": "",
                        "type": "o"
                    },
                    {
                        "t123": ""
                    }
                ],
                [
                    {
                        "t123": "",
                        "type": "o"
                    },
                    {
                        "t123": ""
                    }
                ],
                [
                    {
                        "t123": ""
                    },
                    {
                        "t123": ""
                    },
                    {
                        "t123": ""
                    },
                    {
                        "t123": "",
                        "type": "o"
                    },
                    {
                        "t123": ""
                    },
                    {
                        "t123": ""
                    }
                ]
            ]
        }
    ]
})
ifendou的主页 ifendou | 初学一级 | 园豆:54
提问于:2016-07-01 19:21
< >
分享
最佳答案
0
/// <summary>
/// 泛型反序列化
/// </summary>
/// <typeparam name="T"></typeparam>
/// <param name="strJson"></param>
/// <returns></returns>
public static T JsonDeSerializerObj<T>(string strJson)
{
    T t = JsonConvert.DeserializeObject<T>(strJson);
    return t;
}

比方这个方法,可以将json字符串序列化为Dictionary<string,object>,然后再将不能自动序列化的再次序列化。

另外,json格式是这样的

//1
{"a":1,"b":"s",c:[d:11,dd:"ee"]}

//2
[a:{"a":1,"b":"s",c:[d:11,dd:"ee"]},aa:1,aaa:[{},{}]]

 

收获园豆:140
CodeHsu | 大侠五级 |园豆:5468 | 2016-07-01 21:17
其他回答(4)
0

再贴点代码~

收获园豆:2
幻天芒 | 园豆:37175 (高人七级) | 2016-07-01 20:59
2

你是对于这个json字符串不太会定义class吧。教你一招,吧json10()里面字符串复制。然后vs菜单-》编辑-》选择性粘贴。选择json然后就自动帮你生成class model了。

收获园豆:3
czd890 | 园豆:14412 (专家六级) | 2016-07-01 23:42

public class Rootobject
{
public string[][] ResT { get; set; }
public Mc[] Mc { get; set; }
}

public class Mc
{
public string index { get; set; }
public string type { get; set; }
public Datum[][] data { get; set; }
}

public class Datum
{
public string t123 { get; set; }
public string type { get; set; }
}

支持(1) 反对(0) czd890 | 园豆:14412 (专家六级) | 2016-07-01 23:51
0
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;
using System.Threading.Tasks;

namespace ConsoleApplication1
{
internal class Program
{
private static void Main(string[] args)
{
var jsonp = @"json10({
""ResT"": [
[
""汉"",
""15""
],

[
""高"",
""65""
]
],
""Mc"": [
{
""index"": ""1"",
""type"": ""t_group"",
""data"": [
[
{
""t123"": ""平""
},
{
""t123"": ""坡""
},
{
""t123"": ""平"",
""type"": ""o""
},
{
""t123"": ""中""
}
],
[
{
""t123"": ""厚""
},
{
""t123"": ""真"",
""type"": ""o""
},
{
""t123"": ""学""
},
{
""t123"": ""松""
},
{
""t123"": ""沙""
},
{
""t123"": ""套""
}
]
]
},

{
""index"": ""2"",
""type"": ""t_group"",
""data"": [
[
{
""t123"": ""粗"",
""type"": ""o""
},
{
""t123"": ""中""
}
],
[
{
""t123"": ""水"",
""type"": ""o""
},
{
""t123"": ""水""
}
],
[
{
""t123"": ""平""
},
{
""t123"": ""学""
},
{
""t123"": ""套""
},
{
""t123"": ""大"",
""type"": ""o""
},
{
""t123"": ""头""
},
{
""t123"": ""真""
}
]
]
}
]
})";

Regex regex = new Regex("(\{[\s\S]*\})", RegexOptions.IgnoreCase);
if (regex.IsMatch(jsonp))
{
string value = regex.Match(jsonp).Value;
var root = JsonConvert.DeserializeObject<Rootobject>(value);
}
}
}

public class Rootobject
{
public string[][] ResT { get; set; }
public Mc[] Mc { get; set; }
}

public class Mc
{
public string index { get; set; }
public string type { get; set; }
public Datum[][] data { get; set; }
}

public class Datum
{
public string t123 { get; set; }
public string type { get; set; }
}
}

 

 

代码逻辑:

1、先正则匹配出来里面json

2、然后采用  calvinK 方法转换model

3、调用json.NET反序列化方法

 

如果觉得正则太难,可以采用Replace方法

jsonp.Replace("json10({","{").Repalce("})","")

收获园豆:10
walleyekneel | 园豆:306 (菜鸟二级) | 2016-07-02 23:46
0

首先如过这个json的层数是固定的就直接序列号,不然要定义类。

public static T Parse<T>(string jsonStr)
{
return new JavaScriptSerializer().Deserialize<T>(jsonStr);
}

 

收获园豆:5
Cyclone77 | 园豆:36 (初学一级) | 2016-07-08 14:01
清除回答草稿
   您需要登录以后才能回答,未注册用户请先注册