C#json parsing

Source: Internet
Author: User

JSON sample one:

String jsontext=

{Code": 0,"Message":"Success","Data":{"Boardprovincename":"Shanghai","Boardcityname":"Shanghai","Boarddetailaddress":"No. 449, Lane 8th, Nujiang North Road, Putuo District C5","Couriername":"15601804660","Couriermobile":"15601804660","Cargono": The"Boardno": 3,"Boxno": 2,"Doorno": 3,"Expressno":"333333","Cargopassword": null,"Size": 3,"Status": 2,"Storetime":"\/date (1406772770183)\/","Extractiontime":"\/date (1415185496877)\/","Customermobile":"15601804660","Courierpid":"321323198902082117"}}}

Jobject Jo = Jobject.parse (jsontext); string strcode= jo["Code"]. ToString ();//code:0
String Ddaf = Getjsonvalue (Jo. Children (), "boarddetailaddress");//boarddetailaddress: No. 449 Lane 8th, Nujiang North Road, Putuo District C5

 

=====================================================================

  without = = "["]
String jsondata = "{\" name\ ": \" lily\ ", \" age\ ": 23,\" addr\ ": {\" city\ ": \" guangzhou\ ", \" province\ ": \" guangdong\ "}}" ;            Jobject jsonobj = jobject.parse (jsondata);                      String strname = jsonobj["name"]. ToString ();            String strprovince = Getjsonvalue (Jsonobj.children (), "province");  

=====================================================================

 

Class:

Public  static string Getjsonvalue (Jenumerable<jtoken> Jtoken, string key)        {            IEnumerator Enumerator = Jtoken.getenumerator ();            while (enumerator. MoveNext ())            {                Jtoken JC = (Jtoken) Enumerator. Current;                If (JC is Jobject | | ((Jproperty) JC). Value is Jobject)                {                    return Getjsonvalue (JC. Children (), key);                }                else                {                    if ((Jproperty) JC). Name = = key)                    {                        return (Jproperty) JC). Value.tostring ();            }}} return null;        }

=================================================================================

  with = = "["]
stringJsontext ="{' name ': ' Test ', ' phone ': ' 18888888888 ', ' image ': [{' Name ': ' Img1 ', ' data ': ' data1 '},{' name ': ' Img2 ', ' data ': ' Data2 ' },{' name ': ' Img3 ', ' data ': ' Data3 '}]}"; stringJsonText1 ="{' name ': ' Test ', ' phone ': ' 18888888888 ', ' image ': [{' Name ': ' Img1 ', ' data ': ' Data1 '}]}"; Jobject Jo=(Jobject) jsonconvert.deserializeobject (Jsontext); stringZone = jo["name"].     ToString (); stringZone_en = jo["Phone"].     ToString (); Jarray Jar= Jarray.parse (jo["Image"].    ToString ()); stringc =Zone; stringD =zone_en;  for(vari =0; I < jar. Count; i++) {Jobject J=Jobject.parse (Jar[i].       ToString ()); stringA = j["name"].      ToString (); stringb = j["Data"].    ToString (); }

Multilayer nesting

String jsonData1 = "{\" addr\ ": [{\" city\ ": \" guangzhou\ ", \" province\ ": \" guangdong\ "},{\" city\ ": \" guiyang\ ", \" Province\ ": \" Guizhou\ "}]}";            Jobject jsonObj1 = Jobject.parse (jsonData1);            Jarray jar1 = Jarray.parse (jsonobj1["addr"]. ToString ());            Jobject j0 = Jobject.parse (jar1[0]. ToString ());            Jobject J1 = Jobject.parse (jar1[1]. ToString ());            String city1 = j0["City"]. ToString ();   Guangzhou            String city2 = j0["City"]. ToString ();   Guiyang

  

The above pro-test available

 

C#json parsing

Related Article

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.