2014-03-06 29 views
0

我有一個反序列化一些JSON的問題我從Web服務器中獲得,我認爲這是使用格式化。json DeserializeObject在c#中使用json.net

JSON的是這樣的:

{ 
    "post_count": { 
     "total_posts": 1, 
     "sfw_total_posts": 1, 
     "use": 0 
    }, 
    "posts_per_page": 1, 
    "posts": [ 
     { 
      "guid": 10019127, 
      "wp_id": 656197, 
      "type": "media", 
      "title": "Test", 
      "path": "TestPath", 
      "publish_start": 1385559021, 
      "author": "Test", 
      "web_url": "http://www.test.com", 
      "nsfw": "No", 
      "modified": 1385532803, 
      "video": "No", 
      "likes": 484, 
      "dislikes": 51, 
      "main_category_id": 71, 
      "thumbnails": [ 
       { 
        "w": 120, 
        "h": 120 
       }, 
       { 
        "w": 240, 
        "h": 240 
       } 
      ], 
      "comments": 26 
     } 
    ], 
    "server": "100.200", 
    "time": 0.42163896560669 
} 

我創建了一個類的價值存在,我想使用,然後用

LatestChive lastchives = JsonConvert.DeserializeObject<LatestChive>(jsonstring); 

我嘗試反序列化,但所有的值返回null(我只想要在「帖子」的東西)

如果我嘗試使用「post_count」或「posts_per_page」我可以得到的值不是從「帖子」

我希望這是有道理的,並有一個簡單的解決方法謝謝。

+2

這將幫助,如果你表現出你的'LatestChive'類。 –

回答

3

定義你的類作爲

public class PostCount 
{ 
    public int total_posts { get; set; } 
    public int sfw_total_posts { get; set; } 
    public int use { get; set; } 
} 

public class Thumbnail 
{ 
    public int w { get; set; } 
    public int h { get; set; } 
} 

public class Post 
{ 
    public int guid { get; set; } 
    public int wp_id { get; set; } 
    public string type { get; set; } 
    public string title { get; set; } 
    public string path { get; set; } 
    public int publish_start { get; set; } 
    public string author { get; set; } 
    public string web_url { get; set; } 
    public string nsfw { get; set; } 
    public int modified { get; set; } 
    public string video { get; set; } 
    public int likes { get; set; } 
    public int dislikes { get; set; } 
    public int main_category_id { get; set; } 
    public List<Thumbnail> thumbnails { get; set; } 
    public int comments { get; set; } 
} 

public class LatestChive 
{ 
    public PostCount post_count { get; set; } 
    public int posts_per_page { get; set; } 
    public List<Post> posts { get; set; } 
    public string server { get; set; } 
    public double time { get; set; } 
} 

爲您今後的工作中看到http://json2csharp.com/

+1

謝謝你,先生 –

相關問題