2014-03-06 27 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

谢谢你,先生 –

相关问题