无法反序列化当前的JSON对象

时间:2013-12-25 13:00:32

标签: c# json serialization windows-store-apps json.net

我得到了这个例外。我不明白我该怎么办。我搜索了很多但没有找到任何东西。

  

无法反序列化当前的JSON对象(例如{“name”:“value”})   进入类型   'System.Collections.ObjectModel.ObservableCollection`1 [LU.FacebookStalker.StoreApp.ViewModel.PageLike]'   因为该类型需要一个JSON数组(例如[1,2,3])来反序列化   正确。

     

要修复此错误,请将JSON更改为JSON数组(例如   [1,2,3])或更改反序列化类型,使其成为正常的.NET   type(例如,不是像整数这样的基本类型,不是集合类型   像数组或List一样,可以从JSON对象反序列化。   JsonObjectAttribute也可以添加到类型中以强制它   从JSON对象反序列化。

这是我的班级:

        public class PageLike
        {
            public string Id { get; set; }
            public string Name { get; set; }
            public string count { get; set; }
            public string photoUrl { get; set; }
        }

        public class Hangout
        {
            public string Id { get; set; }
            public string Name { get; set; }
            public string photoUrl { get; set; }
            public int count { get; set; }
        }

        public class Interaction
        {
            public string Id { get; set; }
            public string Name { get; set; }
            public string photoUrl { get; set; }
            public int count { get; set; }
        }

        public class Place
        {
            public object Id { get; set; }
            public string Name { get; set; }
            public string photoUrl { get; set; }
            public int count { get; set; }
        }

        public class HomeSummaryResult
        {
            public List<Hangout> hangouts { get; set; }
            public List<Interaction> interactions { get; set; }
            public List<PageLike> likes { get; set; }
            public List<Place> places { get; set; }
        }

        public class RootObject
        {
            public HomeSummaryResult HomeSummaryResult { get; set; }                
        }

我正在呼叫这样的服务:

HttpClient client = new HttpClient();
string url = string.Format("");             
HttpResponseMessage response = await client.GetAsync(url);
dynamic likesResult= await response.Content.ReadAsStringAsync();           
var x = (JsonConvert.DeserializeObject<IDictionary<string, object>>(likesResult.ToString()))["HomeSummaryResult"];
likesList = JsonConvert.DeserializeObject<ObservableCollection<PageLike>>(x.ToString());

我的JSON是:

{
  "HomeSummaryResult": {
    "hangouts": [
      {
        "Id": "112",
        "Name": "Mohsin",
        "photoUrl": "graph.facebook.com\/112\/picture",
        "count": 12
      },
      {
        "Id": "103",
        "Name": "Khan",
        "photoUrl": "graph.facebook.com\/103\/picture",
        "count": 11
      }
    ],
    "interactions": [
      {
        "Id": "724",
        "Name": "Jawad Shareef",
        "photoUrl": "graph.facebook.com\/724\/picture",
        "count": 482
      },
      {
        "Id": "583",
        "Name": "Ahsan Aziz Abbasi",
        "photoUrl": "graph.facebook.com\/583\/picture",
        "count": 228
      }
    ],
    "likes": [
      {
        "Id": "122",
        "Name": "Community",
        "photoUrl": "graph.facebook.com\/122\/picture",
        "count": 324
      },
      {
        "Id": "110",
        "Name": "Musician\/band",
        "photoUrl": "graph.facebook.com\/110\/picture",
        "count": 119
      }
    ],
    "places": [
      {
        "Id": null,
        "Name": "Local business",
        "photoUrl": "graph.facebook.com\/\/picture",
        "count": 69
      },
      {
        "Id": null,
        "Name": "City",
        "photoUrl": "graph.facebook.com\/\/picture",
        "count": 43
      }
    ]
  }
}

1 个答案:

答案 0 :(得分:1)

所以这就是答案。我自己解决了。

我刚刚添加了["likes"]

代码是:

var x = (JsonConvert.DeserializeObject<IDictionary<string, object>>(likesResult.ToString()))["HomeSummaryResult"]["likes"];

因为json在数组中。