{ "retcode": 0, "result": [
{ "poll_type": "message", "value": { "msg_id": 32065, "from_uin": 2246865592, "to_uin": 1589188359, "msg_id2": 605408, "msg_type": 9, "reply_ip": 178848417, "time": 1352614319, "content": [
[ "font",
{ "size": 10, "color": "000000", "style": [ 0, 0, 0
], "name": "\u9ED1\u4F53"
}
], "hi "
]
}
}
]
}
3 回答
当年话下
TA贡献1890条经验 获得超9个赞
using System;using System.Collections.Generic;using System.Linq;using System.Text;using Newtonsoft.Json;using Newtonsoft.Json.Converters;using Newtonsoft.Json.Serialization;using Newtonsoft.Json.Linq; namespace cscliTest { public class Font { public int size; public int color; public string name; public List<int> style = new List<int>(); } public class Content { public string hi; public Font font = new Font(); } public class Value { public int msg_id; public uint from_uin; public uint to_uin; public int msg_id2; public int msg_type; public int reply_ip; public int time; public List<Content> content = new List<Content>(); } public class Result { public string poll_type; public Value value; } public class sss { public int retcode; public List<Result> result = new List<Result>(); } public class Test { public int m_a; public int a { get; set; } } class Program { static void Main(string[] args) { //测试数据 sss a = new sss(); a.retcode = 0; Result r = new Result(); Value v = new Value(); Content c = new Content(); Font f = new Font(); f.size = 10; f.color = 0; f.style = new List<int>(new int[] { 0, 0, 0 }); f.name = "xxx"; c.font = f; c.hi = "hello"; v.msg_id = 32065; v.from_uin = 2246865592; v.to_uin = 1589188359; v.msg_id2 = 605408; v.msg_type = 9; v.reply_ip = 178848417; v.time = 1352614319; v.content.Add(c); r.value = v; a.result.Add(r); string xx = JsonConvert.SerializeObject(a); Console.WriteLine(xx); //方法一(需定义上面那些类) sss a2 = (sss)JsonConvert.DeserializeObject<sss>(xx); Console.WriteLine(a2.retcode); //方法二(不需定义上面那些类) JObject o = (JObject)JsonConvert.DeserializeObject(xx); JToken o2 = o["result"][0]; JToken o3 = o2["value"]; JToken o4 = o3["content"][0]; JToken o5 = o4["hi"]; Console.WriteLine(o5.ToString()); } } }
感觉content后面的两个[[格式有问题,所以我这里的和你的格式有点差别,请注意。
慕森卡
TA贡献1806条经验 获得超8个赞
var ser = new JavaScriptSerializer();return ser.Deserialize<TResult>(json);
JavaScriptSerializer类在System.Web.Script.Serialization 需引入 System.Web.Extensions dll
牛魔王的故事
TA贡献1830条经验 获得超3个赞
1.引用Newtonsoft.Json
2.写一个反序列化Josn的类
例如我有一个这样的
{ "from": "en", "to": "zh", "trans_result": [ { "src": "today", "dst": "今天" }, { "src": "tomorrow", "dst": "明天" } ] }
对应的反序列化的类
public class TranslateResult { public string from { get; set; } public string to { get; set; } public List<TResult> trans_result{get;set;} } public class TResult { public string src{get;set;} public string dst{get;set;} }
3.然后就是使用了
TranslateResult translateResult = new TranslateResult(); translateResult =JavaScriptConvert.DeserializeObject(Json文本, typeof(TranslateResult))as TranslateResult;
- 3 回答
- 0 关注
- 128 浏览
添加回答
举报
0/150
提交
取消