Newtonsoft. JSON usage

Source: Internet
Author: User

Modify http://www.cnblogs.com/freexiaoyu/archive/2012/08/21/2649333.htmlusage

Or the original JSON format

Model class

View code

 Namespace  Maticsoft. model {  ///   <Summary>      ///  Return JSON attributes  ///   </Summary>  [Serializable]  Public   Class Backinfo <t>{  ///   <Summary>          ///  Status  ///   </Summary>          Public String result { Get ; Set  ;}  ///   <Summary>          ///  Returned data  ///  </Summary>          Public List <t> info { Get ; Set  ;}}  ///   <Summary>      ///  Return JSON attributes  ///   </Summary>  [Serializable]  Public   Class Backdatatable <t> { ///   <Summary>          ///  Status  ///   </Summary>          Public String result { Get ; Set  ;}  ///   <Summary>          ///  Returned data  ///   </Summary>         Public T info { Get ; Set  ;}}} 

Serialize JSON

Classifyinfo = JSON. jsoninfo ("200", jsonconvert. serializeobject (DT); // The dt here is the datatable

 

Deserializing josn

View code

String jsondata = getpage ( "  HTTP: /localhost: 3448/APP/API. aspx  " , "  Action = classifylist & parentid = 2  " ); Maticsoft. model. backdatatable <Datatable> backinfo = (maticsoft. model. backdatatable <datatable>) jsonconvert. deserializeobject (jsondata, Typeof (Maticsoft. model. backdatatable <datatable> ));  If (Backinfo. Result! = "  200  "  ){  //  Failed  } Datatable dt =Backinfo.info;  This . Dropdownlist3.datasource = DT;  This . Dropdownlist3.datatextfield = "  Name  "  ;  This . Dropdownlist3.datavaluefield = "  CID  "  ;  This . Dropdownlist3.databind (); maticsoft. model. backdatatable <List <model. iphone_classify> backinfo1 = (Maticsoft. model. backdatatable <List <model. iphone_classify> ) Jsonconvert. deserializeobject (jsondata,  Typeof (Maticsoft. model. backdatatable <list <model. iphone_classify> ));  If (Backinfo1.result! = "  200  "  ){ //  Failed  }  /*  List <model. iphone_classify> classify = backinfo1.info; this. dropdownlist3.datasource = classify; this. dropdownlist3.datatextfield = "name"; this. Attributes = "CID"; this. dropdownlist3.databind ();  */  Maticsoft. model. backinfo <Model. iphone_classify> backinfo2 = (maticsoft. model. backinfo <model. iphone_classify> ) Jsonconvert. deserializeobject (jsondata,  Typeof (Maticsoft. model. backinfo <model. iphone_classify> ));  If (Backinfo2.result! = "  200  "  ){  //  Failed  }  /* List <model. iphone_classify> classify = backinfo2.info; this. dropdownlist3.datasource = classify; this. dropdownlist3.datatextfield = "name"; this. Attributes = "CID"; this. dropdownlist3.databind ();  */ 

If the serialized JSON directly uses a datatable, The deserialization directly uses

Datatable dt = (datatable) jsonconvert. deserializeobject <datatable> (jsondata );

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.