修改用法
还是原来的JSON格式
Model类
View Code
namespace Maticsoft.Model{ //////返回JSON属性 /// [Serializable] public class BackInfo{ /// /// 状态 /// public String result { get; set; } ////// 返回数据 /// public Listinfo { get; set; } } /// ///返回JSON属性 /// [Serializable] public class BackDataTable{ /// /// 状态 /// public String result { get; set; } ////// 返回数据 /// public T info { get; set; } }}
序列化JSON
Classifyinfo = Json.JsonInfo("200", JsonConvert.SerializeObject(dt)); //这里的dt是datatable
反序列化JOSN
View Code
String JsonData = GetPage("http://localhost:3448/app/api.aspx", "action=ClassifyList&parentID=2"); Maticsoft.Model.BackDataTableBackInfo = (Maticsoft.Model.BackDataTable )JsonConvert.DeserializeObject(JsonData, typeof(Maticsoft.Model.BackDataTable )); if (BackInfo.result != "200") { //失败 } DataTable dt = BackInfo.info; this.DropDownList3.DataSource = dt; this.DropDownList3.DataTextField = "Name"; this.DropDownList3.DataValueField = "CID"; this.DropDownList3.DataBind(); Maticsoft.Model.BackDataTable
> BackInfo1 = (Maticsoft.Model.BackDataTable
>) JsonConvert.DeserializeObject(JsonData, typeof(Maticsoft.Model.BackDataTable
>)); if (BackInfo1.result != "200") { //失败 } /*List classify = BackInfo1.info; this.DropDownList3.DataSource = classify; this.DropDownList3.DataTextField = "Name"; this.DropDownList3.DataValueField = "CID"; this.DropDownList3.DataBind();*/ Maticsoft.Model.BackInfo BackInfo2 = (Maticsoft.Model.BackInfo ) JsonConvert.DeserializeObject(JsonData, typeof(Maticsoft.Model.BackInfo )); if (BackInfo2.result != "200") { //失败 } /*List classify = BackInfo2.info; this.DropDownList3.DataSource = classify; this.DropDownList3.DataTextField = "Name"; this.DropDownList3.DataValueField = "CID"; this.DropDownList3.DataBind();*/
如果序列化JSON直接用的是datatable的话反序列化直接用
DataTable dt=(DataTable)JsonConvert.DeserializeObject<DataTable>(JsonData);