日期:2014-05-18  浏览次数:20738 次

怎样讲JSON反序列化为复杂实体类?
从服务器端返回的这样的一段json串
{
ret:0,
msg:"ok",
errcode:0,
data:{
timestamp:123444,
hasnext:0
info:[
{
name:"aaa",
openid:"B624064BA065E01CB73F835017FE96FA",
nick:"aaa",
head:"xxx",
sex:1,
location:"广东 深圳",
country_code:1,
province_code:44,
city_code:3,
tweet:
{
text:"adg",
from:"来自网页",
id:7987543214334,
timestamp:1285813236
},
fansnum:10,
idolnum:10,
isfans:0,
isvip:0,
tag:[{id:1,name:"aaa"},...],
},
{
name:"aaa",
openid:"B624064BA065E01CB73F835017FE96FA",
nick:"bbb",
head:"xxx",
sex:2,
location:"广东 深圳",
tweet:
{
text:"dgfgh",
from:"来自网页",
id:7987543214334,
timestamp:1285813236
}
fansnum:10,
idolnum:10,
isfans:0,
isvip:0,
tag:[{id:1,name:"bbb"},...],
}
......
]
}
}
怎样将他反序列化呢?尝试了一个下午,用了 Newtonsoft.Json 和 .Net 自带的类库System.Runtime.Serialization.Json;都只能反序列化为简单的实体类。在用Newtonsoft.Json反序列化时会出现如下错误:
Cannot deserialize JSON object into type 'System.Collections.Generic.List`1[TXWeibo.info]'.

我定义的实体类为:

C# code

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Runtime.Serialization.Json;
using System.Runtime.Serialization;
namespace TXWeibo
{
    [DataContractAttribute]
    public class user
    {
        [DataMember(Name = "data")]
        public List<info> data{ get; set; }
        [DataMember]
        public int errcode { get; set; }
        [DataMember]
        public string msg { get; set; }
        [DataMember]
        public string ret { get; set; }
        [DataMember]
        public int nextstartpos { get; set; }
        
    }

    [DataContract]
    public class info
    {
        [DataMember]
        public string city_code { get; set; }
        [DataMember]
        public string country_code { get; set; }
        [DataMember]
        public int fansnum { get; set; }
        [DataMember]
        public string head { get; set; }
        [DataMember]
        public int idolnum { get; set; }
        [DataMember]
        public bool isfans { get; set; }
        [DataMember]
        public bool isidol { get; set; }
        [DataMember]
        public int isrealname { get; set; }
        [DataMember]
        public int isvip { get; set; }
        [DataMember]
        public string location { get; set; }
        [DataMember]
        public string name { get; set; }
        [DataMember]
        public string nick { get; set; }
        [DataMember]
        public string openid { get; set; }
        [DataMember]
        public string province_code { get; set; }
        [DataMember]
        public int sex { get; set; }
        [DataMember]
        public List<Tag> tag { get; set; }
        [DataMember]
        public Tweet tweet { get; set; }
        [DataMember]
        public int nextstartpos { get; set; }
        [DataMember]
        public int timestamp { get; set; }
    }

    [DataContract]
    public class Tag
    {
        [DataMember]
        public string id { get; set; }
        [DataMember]
        public string name { get; set; }
    }

    [DataContract]
    public class Tweet
    {
        [DataMember]
        public string from { get; set; }
        [DataMember]
        public string id { get; set; }
        [DataMember]
        public string text { get; set; }
        [DataMember]
        public int timestamp { get; set; }
    }

}