JSON 数组转成.NET对象 求救!!! 在线等!
json 数组
{
"total":23000,
"count":10000,
"data":{
"openid":[
"OPENID10001",
"OPENID10002",
...,
"OPENID20000"
]
},
"next_openid":"NEXT_OPENID2"
}
实体类
[Serializable]
public class Users
{
public string total{get;set;}
public int count { get; set; }
public Data data { get; set; }
public string next_openid { get; set; }
}
public class Data
{
public string openid { get; set; }
}
把JSON字符串转成对象
public static object GetObjectByJson(string jsonString, object obj)
{
try
{
DataContractJsonSerializer serializer = new DataContractJsonSerializer(obj.GetType());
using (MemoryStream mStream = new MemoryStream(Encoding.GetEncoding("gb2312").GetBytes(jsonString)))
{
return serializer.ReadObject(mStream);
}
}
catch (Exception ex)
{
throw ex;
}
}
现在的请况是,把JSON中的DATA 数组定义成string[] data,就可以得到 "total":23000,
"count":10000, 和值,定义成 实体类data 就直接报错了。
报错提示:无法反序列化,因为未找到必须的数据成员
------解决方案--------------------public class Data
{
public string[] openid { get; set; }
}
*****************************************************************************
签名档: http://feiyun0112.cnblogs.com/
------解决方案--------------------
JavaScriptSerializer js=new JavaScriptSerializer();
Users user= js.Deseri