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