C# JSON 转换成 LIST对象,list对象为空值

chm135229 2013-02-24 07:37:34
我把JSON转换成LIST对象,但就是LIST里的数据为空,请高手指一下,代码如下:
JSON数据为:[{"xh":"0","xm":"AAA","xb":"男","csny":"1919-01-01"},{"xh":"1","xm":"AAA","xb":"男","csny":"1919-01-01"},{"xh":"2","xm":"AAA","xb":"男","csny":"1919-01-01"}]

System.IO.StreamReader streamReade = new System.IO.StreamReader(stream);
json = streamReade.ReadToEnd();
System.IO.MemoryStream ms = new System.IO.MemoryStream(Encoding.UTF8.GetBytes(json));
List<jsonclass> lis = new List<jsoss>();

DataContractJsonSerializer ser = new DataContractJsonSerializer(lis.GetType());

lis = (List<jsonclass>) ser.ReadObject(ms);
context.Response.ContentType = "text/plain";


context.Response.Write(lis[0].xm); 为什么这里读出来的数成为空


[Serializable]
public class jsonclass:List<Object>
{
//[DataMember]
public string xh{get;set;}
//[DataMember]
public string xm{get;set;}
// [DataMember]
public string xb{get;set;}
// [DataMember]
public string csny{get;set;}


}
...全文
1084 3 打赏 收藏 转发到动态 举报
写回复
用AI写文章
3 条回复
切换为时间正序
请发表友善的回复…
发表回复
  • 打赏
  • 举报
回复


//list类名
public class jsonclass
{
    public  string xh{get;set;}
    public  string xm{get;set;}
    public  string xb{get;set;}
    public  string csny{get;set;}
}

//调用时:json数据
//需要引用using System.Web.Script.Serialization;微软自带的
JavaScriptSerializer Serializer = new JavaScriptSerializer();
List<jsonclass> jsonclassList = Serializer.Deserialize<List<jsonclass>>(json);

chm135229 2013-03-04
  • 打赏
  • 举报
回复
不行啊。。不知道为什么
Jia_H 2013-03-02
  • 打赏
  • 举报
回复
你如果确认编码等处理都没有问题的话。在ReadObject的前面添加 ms.Position = 0; 保证从开始读取,应该就好了。你现在的代码ms.Position是在最后结束的地方。打个断点看一下就知道是不是这样的。

1,978

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术 其他语言讨论
社区管理员
  • 其他语言社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧