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