2015-06-16 203 views
1

這就是我的Json如何解析這個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對象反序列化。 JsonObjectAttribute也可以添加到類型中,以強制它從JSON對象反序列化。 路徑 '計數',1號線,9號位置

+1

您可以使用http://json2csharp.com/非常輕鬆地將您的Json轉換爲課程 –

+0

在您處於此狀態時向我發送您的社會安全號碼,對嗎? – TheMCProgrammer

回答

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解析器做爲你解析。

開始解決這些基本問題,並看看你如何繼續。

+0

而且應該線程是一個JsonObject? – tjleigh

+0

假設你可以像你那樣將它反序列化成一個類,它並不需要是一個'JsonObject'。 – Lloyd

+0

我同意這比較JSON到C#類似乎錯了,但錯誤消息看起來像這個問題的*相反*。 – Rawling