弄了一天了,没有弄出来,
打算用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": "真" } ] ] } ] })
/// <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:[{},{}]]
再贴点代码~
你是对于这个json字符串不太会定义class吧。教你一招,吧json10()里面字符串复制。然后vs菜单-》编辑-》选择性粘贴。选择json然后就自动帮你生成class model了。
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; }
}
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("})","")
首先如过这个json的层数是固定的就直接序列号,不然要定义类。
public static T Parse<T>(string jsonStr) { return new JavaScriptSerializer().Deserialize<T>(jsonStr); }