Skip to main content
 首页 » 编程设计

c#之如何解析这个 JSON

2024年02月06日13落叶无声

这是我的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; } 
} 

但是我有错误

Cannot deserialize the current JSON object (e.g. {"name":"value"}) into type 'System.Collections.Generic.List`1[AppHandHeld.Class.Threads]' because the type requires a JSON array (e.g. [1,2,3]) to deserialize correctly. To fix this error either change the JSON to a JSON array (e.g. [1,2,3]) or change the deserialized type so that it is a normal .NET type (e.g. not a primitive type like integer, not a collection type like an array or List) that can be deserialized from a JSON object. JsonObjectAttribute can also be added to the type to force it to deserialize from a JSON object. Path 'count', line 1, position 9.

请您参考如下方法:

这是一个开始的问题:

[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 解析器为您进行解析。

开始解决这些基本问题,看看进展如何。