0

Im having a problem deserializing some json I get from a webserver and I think it is beacuse of the formating.

The json look like this :

{
    "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
}

I have created a class with the value for there that I want to use and then using

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

I try to deserialize it but all the values return null (I only want the stuff that is in "posts")

If I try with "post_count" or "posts_per_page" i can get the values just not from the the "posts"

I hope this makes sense and there is a easy fix thank you.

1
  • 2
    It would help if you showed your LatestChive class. Commented Mar 6, 2014 at 17:01

1 Answer 1

3

Define your classes as

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; }
}

For your future work see http://json2csharp.com/

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.