使用到的第三方插件Json.NET。dll文件下载见下方附件。
using Newtonsoft.Json;
using Newtonsoft.Json.Linq
/下面为解析核心代码
//实体转JSON
Student stu = new Student { Name = "duweibin",Age = 23};
textBlock1.Text = JsonConvert.SerializeObject(stu);
//{'Name':'duweibin','Age':23}
//JSON转实体(键不区分大小写)
string jsondata = "{'name':'duweibin','age':23}";
Student stu2 = JsonConvert.DeserializeObject<Student>(jsondata);
textBlock2.Text = stu2.Name;
//一一解析
JObject jobject = JObject.Parse(jsondata);
textBlock2.Text = jobject["age"].ToString();
//数组解析
string arrJson = "[{'one':'111'},{'two':'222'}]";
JArray arr = JArray.Parse(arrJson);
JObject a1 = (JObject)arr[0];
textBlock2.Text = a1["one"].ToString();
//实体类
public class Student
{
public string Name
{
get;
set;
}
public int Age
{
get;
set;
}
}