1.JSONConvert 序列化与反序列化
string js=JSONConvert.SerialObject(object obj) 对象序列化成JSON格式字符串
例子:string js=JSONConvert.SerailObject(new{Name="zhangsan",Age=22});
结果: {"Name":"zhangsan","Age":22};
反序列化:
T obj=JSONConvert.DeserializeObject(@"{ 'Name':'zhangsan','Age':22}")
textBox1.Text=obj.Name;
// zhangsan
2.JSON.Linq
public static JObject JObject.Parse(string json)
JObject jo=JObject.Parse(string jsonString);
例子:
JObject jo = JObject.Parse(@"{'Name':'sundh','Age':25}");
textBox1.AppendText(jo["Name"].ToString());
结果:sundh
JArray ja=JArray.Parse(@"['aa','bb','cc']");
textBox1.Text=ja.ToString();
//结果:["aa","bb","cc"]
2.1 SelectToken(string item)
JObject jo = JObject.Parse(@"{'Student':{'Name':'zhangsan','Age':'15'},'Score':{'YuWen':89,'ShuXue':67,'YingYu':95}}");
JObject jStudent = jo.SelectToken("Student") as JObject; //方法1 返回值为JToken
JObject js = jo["Student"] as JObject; //方法2 返回值为:JToken 因此需要转换
textBox1.Text = jStudent.ToStr