Convert a JSON-formatted string into an object

Source: Internet
Author: User

(1)

       Private voidButton1_Click (Objectsender, EventArgs e) {StringBuilder SB=NewStringBuilder (); Sb. Append ("{"); Sb. Append ("\ "search\": null,"); Sb. Append ("\ "totalcount\": 2,"); Sb. Append ("\ "list\": ["); Sb. Append ("{"); Sb. Append ("\ "id\": \ "De\","); Sb. Append ("\ "name\": \ "Herz\""); Sb. Append ("},"); Sb. Append ("{"); Sb. Append ("\ "id\": \ "E_0\","); Sb. Append ("\ "name\": \ "Sciences\""); Sb. Append ("}"); Sb. Append ("],"); Sb. Append ("\ "indexid\": null,"); Sb. Append ("\ "Success\": True"); Sb. Append ("}"); //parse the JSON object into a class, noting that the name and data type must match. Easy way: vs "edit"--"Paste as Class"--automatically generated            #regionMethod One:newtonsoft.json.jsonconvert.deserializeobject<>Classobject Co= newtonsoft.json.jsonconvert.deserializeobject<classobject>(sb.)            ToString ()); MessageBox.Show (Co.list.Length.ToString ());//Output 2            #endregion            #regionMethod Two: System.Runtime.Serialization.Json.DataContractJsonSerializer//Storage StreamSystem.IO.MemoryStream Mstream =NewSystem.IO.MemoryStream (Encoding.UTF8.GetBytes (sb.)            ToString ())); //specifying a deserialization objectSystem.Runtime.Serialization.Json.DataContractJsonSerializer serializer =NewSystem.Runtime.Serialization.Json.DataContractJsonSerializer (typeof(Classobject)); //to parseClassobject Cccoo =(classobject) serializer.            ReadObject (Mstream); MessageBox.Show (Cccoo.list.Length.ToString ());//Output            #endregion        }        //Computer generated:object Classobject and list based on JSON-formatted string parsing         Public classClassobject { Public ObjectSearch {Get;Set; }  Public intTotalCount {Get;Set; }  PublicList[] List {Get;Set; }  Public ObjectIndexID {Get;Set; }  Public BOOLSuccess {Get;Set; } }         Public classList { Public stringID {Get;Set; }  Public stringName {Get;Set; } }

(2) However, the computer automatically generated objects, in the parsing is not all can be resolved successfully. As follows

        Private voidButton1_Click (Objectsender, EventArgs e) {StringBuilder SB=NewStringBuilder (); Sb. Append ("["); Sb. Append ("{"); Sb. Append ("\ "code\": \ "Bl\","); Sb. Append ("\ "title\": \ "mob \""); Sb. Append ("},"); Sb. Append (" {"); Sb. Append ("\ "code\": \ "Cp\","); Sb. Append ("\ "title\": \ "foreign language \""); Sb. Append ("},"); Sb. Append ("{"); Sb. Append ("\ "code\": \ "Gp\","); Sb. Append ("\ "title\": \ "calculation \""); Sb. Append ("}"); Sb. Append ("]"); List<CodeTitle> ctlist = newtonsoft.json.jsonconvert.deserializeobject<list<codetitle>>(sb.)             ToString ()); MessageBox.Show (ctlist. Count.tostring ());//Output 3//here is an error: The computer automatically generated objects are not all right. Rootobject r = newtonsoft.json.jsonconvert.deserializeobject<rootobject>(sb.)        ToString ()); }        //manually written by yourself: Classobject and List of objects parsed according to a JSON-formatted string        classCodetitle { Public stringCode {Get;Set; }  Public stringTitle {Get;Set; } }        //Rootobject and Class1 automatically generated by computers         Public classRootobject { PublicClass1[] Property1 {Get;Set; } }         Public classClass1 { Public stringCode {Get;Set; }  Public stringTitle {Get;Set; } }

Finish!

Convert a JSON-formatted string into an object

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.