{ "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 回答
當(dāng)年話下
TA貢獻(xiàn)1890條經(jīng)驗(yàn) 獲得超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)
{
//測試數(shù)據(jù)
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后面的兩個[[格式有問題,所以我這里的和你的格式有點(diǎn)差別,請注意。
慕森卡
TA貢獻(xiàn)1806條經(jīng)驗(yàn) 獲得超8個贊
var ser = new JavaScriptSerializer();return ser.Deserialize<TResult>(json);
JavaScriptSerializer類在System.Web.Script.Serialization 需引入 System.Web.Extensions dll
牛魔王的故事
TA貢獻(xiàn)1830條經(jīng)驗(yàn) 獲得超3個贊
1.引用Newtonsoft.Json
2.寫一個反序列化Josn的類
例如我有一個這樣的
{
"from": "en", "to": "zh", "trans_result": [
{
"src": "today", "dst": "今天"
},
{
"src": "tomorrow", "dst": "明天"
}
]
}對應(yīng)的反序列化的類
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 關(guān)注
- 183 瀏覽
添加回答
舉報(bào)
0/150
提交
取消
