C # Processing json data

Source: Internet
Author: User

I just got started with json and found it interesting. I just used c # as an example to play with it. Here I will record it.

Json data format:

{"Id": 20547, "Name": "central south university", "NameEn": "central-south university", "AreaId": 190 },{ "Id ": 20548, "Name": "Hunan university", "NameEn": "Hunan university", "AreaId": 190 },{ "Id": 20549, "Name ": "hunan normal university", "NameEn": "hunan normal university", "AreaId": 190}

 

First, let's introduce the object class. Here, we should remember that the class name and json should be consistent in case and case.

Public class School
{
[System. Runtime. Serialization. DataMember]
Public string Id {get; set ;}
[System. Runtime. Serialization. DataMember]
Public string Name {get; set ;}
[System. Runtime. Serialization. DataMember]
Public string NameEn {get; set ;}
[System. Runtime. Serialization. DataMember]
Public string AreaId {get; set ;}
}

 

For more information about how to obtain a single result set, see other people's statement:
Public static School getOneJson (string jsonText)
{
Using (MemoryStream MS = new MemoryStream (Encoding. UTF8.GetBytes (jsonText )))
{
DataContractJsonSerializer serializer = new DataContractJsonSerializer (typeof (School ));
Return (School) serializer. ReadObject (MS );
}

}

 

After finding multiple items, you will not be too lazy to find them. You can find them at will. If you can get the value and efficiency, you can still study it later.

Public static List <School> getOneJson (string jsonText)
{

List <School> list = new List <School> ();
JavaScriptSerializer js = new JavaScriptSerializer ();
Object obj = js. DeserializeObject (jsonText );
Foreach (object item in (object []) (js. DeserializeObject (jsonText ))))
{
School model = new School ();
Model. Id = (Dictionary <string, object>) item) ["Id"]. ToString ();
Model. Name = (Dictionary <string, object>) item) ["Name"]. ToString ();
Model. NameEn = (Dictionary <string, object>) item) ["NameEn"]. ToString ();
Model. AreaId = (Dictionary <string, object>) item) ["AreaId"]. ToString ();
List. Add (model );
}

Return list;

}

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.