C # parse the Data Summary In Json format,
Recently, we have encountered some data in different Json formats, which requires different processing to be converted to the desired result. Here we will summarize it.
First form: status is in the {} form
String json = @ "{'name': 'meaning Shen', 'vince ': '32', 'city': '1', 'location ': 'nanjing Jiangsu ', 'status': {'created _ at': 'thu Feb 26 21:04:34 + 0800 2015', 'text': 'hahaha '}}";
For the data in the preceding format, create the following two struct or classes
1 public struct Status 2 { 3 public string created_at { get; set; } 4 public string text { get; set; } 5 } 6 7 public struct JsonData 8 { 9 public string name { get; set; }10 public string province { get; set; }11 public string city { get; set; }12 public string location { get; set; }13 public Status status;14 }
Output result:
1 JavaScriptSerializer jsSerializer=new JavaScriptSerializer();2 JsonData jd = jsSerializer.Deserialize<JsonData>(json);3 Response.Write(string.Format("name={0};province={1};city={2};location={3};status={4};",jd.name,jd.province,jd.city,jd.location,jd.status.created_at+jd.status.text));
Second form: status is in the [] Form
String json = @ "{'name': 'meaning Shen', 'vince ': '32', 'city': '1', 'location ': 'nanjing Jiangsu ', 'status': [{'created _ at': 'thu Feb 26 21:04:34 + 0800 123', 'text': 'haha'}]} ";
For the data in the preceding format, create the following two struct or classes
1 public struct Status 2 { 3 public string created_at { get; set; } 4 public string text { get; set; } 5 } 6 public struct JsonData2 7 { 8 public string name { get; set; } 9 public string province { get; set; }10 public string city { get; set; }11 public string location { get; set; }12 public List<Status> status;13 }
Output result
1 JavaScriptSerializer jsSerializer=new JavaScriptSerializer();2 JsonData2 jd = jsSerializer.Deserialize<JsonData2>(json);3 Response.Write(string.Format("name={0};province={1};city={2};location={3};status={4};",jd.name,jd.province,jd.city,jd.location,jd.status[0].created_at+jd.status[0].text));