如何解析这个JSON

时间:2015-06-16 08:40:18

标签: c# json json.net

这是我的Json

{
   "count":2,
   "threads":[
      {
         "thread_id":346568,
         "node_id":75,
         "title":"Gi\u1ea3i      ph\u00e1p th\u1ed5i bay m\u00f9i h\u00f4i n\u00e1ch t\u1ef1 tin h\u01a1n trong ng\u00e0y h\u00e8",
         "reply_count":0,
         "view_count":2,
         "user_id":339597,
         "username":"giangdaigia20",
         "post_date":1434435728,
         "sticky":0,
         "discussion_state":"visible",
         "discussion_open":1,
         "discussion_type":"",
         "first_post_id":2468576,
         "first_post_likes":0,
         "last_post_date":1434435728,
         "last_post_id":2468576,
         "last_post_user_id":339597,
         "last_post_username":"giangdaigia20",
         "prefix_id":2,
         "block_adsense":0,
         "thumbnail_url":"",
         "thumbnail_cache_waindigo":"a:8:{s:13:\"thumbnail_url\";s:69:\"http:\/\/trihoinach.org\/wp-content\/uploads\/2015\/04\/Tri-hoi-nach5555.jpg\";s:12:\"thumbnailUrl\";s:69:\"http:\/\/trihoinach.org\/wp-content\/uploads\/2015\/04\/Tri-hoi-nach5555.jpg\";s:5:\"width\";s:2:\"48\";s:6:\"height\";s:2:\"48\";s:9:\"max-width\";s:2:\"48\";s:10:\"max-height\";s:2:\"48\";s:17:\"vertical-position\";i:0;s:19:\"horizontal-position\";i:0;}",
         "custom_fields":[

         ],
         "socia    l_forum_id":0,
         "live_waindigo":0,
         "current_event_id_waindigo":0,
         "google_event_id_waindigo":"",
         "social_forum_title":null,
         "social_forum_user_id":null,
         "social_forum_style_id":null,
         "absolute_url":"http:\/\/dev.handheld.vn\/threads\/346568\/"
      },
      {
         "thread_id":346567,
         "node_id":85,
         "title":"Nh\u1edd c\u00e1c b\u00e1c  t\u01b0 v\u1ea5n d\u00f9m em 2 c\u00e1i \u0111\u1ed3ng h\u1ed3 Citizen n\u00e0y",
         "reply_count":1,
         "view_count":4,
         "user_id":156695,
         "username":"gamap",
         "post_date":1434430984,
         "sticky":0,
         "discussion_state":"visible",
         "discussion_open":1,
         "discussion_type":"",
         "first_post_id":2468575,
         "first_post_likes":0,
         "last_post_date":1434443484,
         "last_post_id":2468577,
         "last_post_user_id":156695,
         "last_post_username":"gamap",
         "prefix_id":95,
         "block_adsense":0,
         "thumbnail_url":"",
         "thumbnail_cache_waindigo":"a:8:{s:13:\"thumbnail_url\";s:86:\"http:\/\/i16.photobucket.com\/albums\/b3\/vozmember\/shopdongho\/citizen\/real\/BU0011-55Aa.jpg\";s:12:\"thumbnailUrl\";s:86:\"http:\/\/i16.photobucket.com\/albums\/b3\/vozmember\/shopdongho\/ci    tizen\/real\/BU0011-55Aa.jpg\";s:5:\"width\";s:2:\"48\";s:6:\"height\";s:2:\"48\";s:9:\"max-width\";s:2:\"48\";s:10:\"max-height\";s:2:\"48\";s:17:\"vertical-position\";i:0;s:19:\"horizontal-position\";i:0;}",
         "custom_fields":[

         ],
         "social_forum_id":0,
         "live_waindigo":0,
         "current_event_id_waindigo":0,
         "google_event_id_waindigo":"",
         "social_forum_title":null,
         "social_forum_user_id":null,
         "social_forum_style_id":null,
         "absolute_url":"http:\/\/dev.handheld.vn\/threads\/34656 7\/"
      }
   ]
}

这是我的班级定义

class Thread_Result
{

    public string count { get; set; }
    [JsonProperty("threads")]
    public Threads threads { get; set; }

}

class Threads
{
     [JsonProperty("thread_id")]
    public string thread_id { get; set; }
     [JsonProperty("node_id")]
    public string node_id { get; set; }
      [JsonProperty("title")]
    public string title { get; set; }
      [JsonProperty("reply_count")]
    public string reply_count { get; set; }
      [JsonProperty("view_count")]
    public string view_count { get; set; }
      [JsonProperty("user_id")]
    public string user_id { get; set; }
      [JsonProperty("post_date")]
    public string post_date { get; set; }
      [JsonProperty("sticky")]
    public string sticky { get; set; }
      [JsonProperty("discussion_state")]
    public string discussion_state { get; set; }
     [JsonProperty("discussion_open")]
    public string discussion_open { get; set; }
      [JsonProperty("discussion_type")]
    public string discussion_type { get; set; }
       [JsonProperty("first_post_id")]
    public string first_post_id { get; set; }
      [JsonProperty("first_post_likes")]
    public string first_post_likes { get; set; }
       [JsonProperty("last_post_date")]
    public string last_post_date { get; set; }
     [JsonProperty("last_post_id")]
    public string last_post_id { get; set; }
      [JsonProperty("last_post_user_id")]
    public string last_post_user_id { get; set; }
       [JsonProperty("last_post_username")]
    public string last_post_username { get; set; }
       [JsonProperty("prefix_id")]
    public string prefix_id { get; set; }
       [JsonProperty("block_adsense")]
    public string block_adsense { get; set; }
      [JsonProperty("thumbnail_url")]
    public string thumbnail_url { get; set; }
      [JsonProperty("thumbnail_cache_waindigo")]
    public string thumbnail_cache_waindigo { get; set; }
     [JsonProperty("custom_fields")]
    public string custom_fields { get; set; }
    [JsonProperty("social_forum_id")]
    public string social_forum_id { get; set; }
     [JsonProperty("live_waindigo")]
    public string live_waindigo { get; set; }
     [JsonProperty("current_event_id_waindigo")]
    public string current_event_id_waindigo { get; set; }
        [JsonProperty("google_event_id_waindigo")]
    public string google_event_id_waindigo { get; set; }
          [JsonProperty("social_forum_title")]
    public string social_forum_title { get; set; }
         [JsonProperty("social_forum_user_id")]
    public string social_forum_user_id { get; set; }
         [JsonProperty("social_forum_style_id")]
    public string social_forum_style_id { get; set; }
         [JsonProperty("absolute_url")]
    public string absolute_url { get; set; }
}

但我收到了错误

  

无法将当前JSON对象(例如{“name”:“value”})反序列化为类型'System.Collections.Generic.List`1 [AppHandHeld.Class.Threads]',因为该类型需要JSON数组(例如[1,2,3])正确反序列化。   要修复此错误,请将JSON更改为JSON数组(例如[1,2,3])或更改反序列化类型,使其成为普通的.NET类型(例如,不是像整数这样的基本类型,而不是类似的集合类型可以从JSON对象反序列化的数组或List。 JsonObjectAttribute也可以添加到类型中以强制它从JSON对象反序列化。   路径'计数',第1行,第9位。

1 个答案:

答案 0 :(得分:4)

这是一个开始的问题:

[JsonProperty("threads")]
public Threads threads { get; set; }

解析器的内容如下:

{
    "threads": { <single thread object> }
}

需要:

[JsonProperty("threads")]
public Threads[] threads { get; set; }

看起来像:

{
   "threads": [<array of thread objects>]
}

您提供的JSON示例中的内容如何。然后你也有:

[JsonProperty("custom_fields")]
public string custom_fields { get; set; }

这在JSON中意味着什么:

{
    "custom_fields": "<some string>"
}

然而,在JSON中它实际上是:

{
    "custom_fields": [<an array>]
}

应该是这样的:

[JsonProperty("custom_fields")]
public string[] custom_fields { get; set; }

你再次进入Thread_Result

[JsonProperty("count")]
public string Count { get; set; }

这应该是:

[JsonProperty("count")]
public int Count { get; set; }

事实上,您已经完成了几乎所有数值的操作,尝试使用正确的值类型,让JSON解析器为您进行解析。

开始修复这些基本问题,看看你是如何继续的。