无法从REST服务反序列化Json

时间:2011-02-19 07:43:33

标签: json windows-phone-7 deserialization

我最难反序列化json字符串。

我正在使用RestSharp api,如果我在请求中指定了RootElement,那该方法效果很好。然后我转移到Hammock以获取OAuth功能,但反序列化并不那么容易。

我尝试过使用DataContractJsonSerializer

  

DataContractJsonSerializer ser = new DataContractJsonSerializer(typeof(List));     var member =(List)ser.ReadObject(response.ContentStream);

但是这给了我一个InvalidCastException。

我试过JsonConvert

var members = JsonConvert.DeserializeObject<List<Member>>(response.Content);

但是我得到了异常:无法将JSON对象反序列化为类型'System.Collections.Generic.List`1 [Member]'。

RestSharp在调用ExecuteAsync时很容易解决这个问题。 Client.ExecuteAsync<List<Member>>(request, (response) =>

我的智慧结束了。 也许我需要一个等同于RestSharp的RootElement属性的Hammock ?? 这只是我的Json很难转换吗?

这是我的会员对象

public partial class Member 
{
    public string State { get; set; }
    public string Joined { get; set; }
    public string lat { get; set; }
    public string Zip { get; set; }
    public string Bio { get; set; }
    public string Name { get; set; }
    public string City { get; set; }
    public string Id { get; set; }
    public string Link { get; set; }
    public string Country { get; set; }
    public string Visited { get; set; }
    public string Photo_url { get; set; }
    public string lon { get; set; }
}

这是Json:

{
    "results": [
        {
            "zip": "11111",
            "lon": "-122.22000122070312",
            "photo_url": "http: //photos1.aaaaa.com/photos/member/1/6/c/e/member_4469838.jpeg",
            "link": "http: //www.aaaa.com/members/7804365",
            "state": "AA",
            "lang": "en_US",
            "city": "MyCity",
            "country": "us",
            "id": "7804365",
            "visited": "Sat Feb 19 02: 36: 40 EST 2011",
            "topics": [
                {
                    "id": 3340,
                    "urlkey": "pickupsoccer",
                    "name": "Pick-up Soccer"
                },
                {
                    "id": 468,
                    "urlkey": "dads",
                    "name": "Dads"
                },
                {
                    "id": 20557,
                    "urlkey": "coed-soccer",
                    "name": "Coed Soccer"
                },
                {
                    "id": 148421,
                    "urlkey": "windowsphone",
                    "name": "Windows Phone"
                }
            ],
            "joined": "Thu Aug 07 15: 32: 06 EDT 2008",
            "bio": "",
            "name": "aaa bbbb",
            "other_services": {
                "linkedin": {
                    "identifier": "http: //www.bbb.com/in/zzzzz"
                }
            },
            "lat": "47.790000915527344"
        }
    ],
    "meta": {
        "lon": "",
        "count": 1,
        "link": "https: //api.aaaaa.com/members",
        "next": "",
        "total_count": 1,
        "url": "https: //api.aaaaa.com/members?relation=self&order=name&offset=0&format=json&page=800",
        "id": "",
        "title": "Members",
        "updated": "Fri Sep 10 13: 08: 07 EDT 2010",
        "description": "API method",
        "method": "Members",
        "lat": ""
    }
}

更新 为我的Member类添加一个封装整个json结果的包装器对象。

public partial class Members
{
    public List<Member> results { get; set; }
    public object meta { get; set; }
}

然后我可以使用以下内容反序列化:

var members = JsonConvert.DeserializeObject<Members>(jsonstring);

3 个答案:

答案 0 :(得分:3)

您的JSON数据中有三个问题区域:

  1. 在这里呈现的方式,它在开头和结尾包含大量反斜杠和双引号,但在JSON中不起作用。从您的问题中很难判断这是否是从VisualStudio调试器复制它的工件,或者实际上是数据中的问题。

  2. 传输的数据不是 Member 实例的列表,而是包含成员实例列表和一些其他元信息的对象。因此,您需要引入一个包含两个成员结果 meta 的附加类。

  3. 您的班级会员使用以大写字母开头的属性,而JSON数据使用全部小写字母。您可以更改属性名称或使用数据 DataMember annoation:

  4. 所以解决办法可能是:

    DataContractJsonSerializer ser = new DataContractJsonSerializer(typeof(JsonResponse));
    JsonResponse jsonResponse = (JsonResponse)ser.ReadObject(response.ContentStream);
    

    具有以下类定义:

    [DataContract]
    public partial class Member 
    {
        [DataMember(Name = "state")]
        public string State { get; set; }
        [DataMember(Name = "joined")]
        public string Joined { get; set; }
        [DataMember(Name = "lat")]
        public string Lat { get; set; }
        [DataMember(Name = "zip")]
        public string Zip { get; set; }
        [DataMember(Name = "bio")]
        public string Bio { get; set; }
        [DataMember(Name = "name")]
        public string Name { get; set; }
        [DataMember(Name = "state")]
        public string City { get; set; }
        [DataMember(Name = "city")]
        public string Id { get; set; }
        [DataMember(Name = "link")]
        public string Link { get; set; }
        [DataMember(Name = "country")]
        public string Country { get; set; }
        [DataMember(Name = "visited")]
        public string Visited { get; set; }
        [DataMember(Name = "photo_url")]
        public string PhotoUrl { get; set; }
        [DataMember(Name = "lon")]
        public string Lon { get; set; }
    }
    
    [DataContract]
    public class Meta
    {
        [DataMember(Name = "lon")]
        public string Lon { get; set; }
        [DataMember(Name = "count")]
        public int Count { get; set; }
        [DataMember(Name = "link")]
        public string Link { get; set; }
        [DataMember(Name = "next")]
        public string Next { get; set; }
        [DataMember(Name = "total_count")]
        public int TotalCount { get; set; }
    }
    
    [DataContract]
    public class JsonResponse
    {
        [DataMember(Name = "results")]
        public List<Member> Results { get; set; }
        [DataMember(Name = "meta")]
        public Meta Meta { get; set; }
    }
    

答案 1 :(得分:2)

在我们的项目中,我们使用Hammock,你可以尝试修改你的类:

[DataContract]
public partial class Member 
{
   [DataMember(Name="zip")]
   public string Zip { get; set; }
   [DataMember(Name="photo_url")]
   public string Photo_url { get; set; }
   //Etc.
}

答案 2 :(得分:0)