C # parse the Data Summary In Json format,

Source: Internet
Author: User

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));

 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.