Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Json.Net deserialize JSON objects with index as name [duplicate]

I am attempting to parse JSON from a web service using Json.NET, the web service returns data in the following format:

{
    "0": {
        "ID": 193,
        "Title": "Title 193",
        "Description": "Description 193",
        "Order": 5,
        "Hyperlink": "http://someurl.com"
    },
    "1": {
        "ID": 228,
        "Title": "Title 228",
        "Description": "Description 228",
        "Order": 4,
        "Hyperlink": "http://someurl.com"
    },
    "2": {
        "ID": 234,
        "Title": "Title 234",
        "Description": "Description 234",
        "Order": 3,
        "Hyperlink": "http://someurl.com"
    }
}

I used json2sharp to generate a class from the JSON:

public class __invalid_type__0
{
    public int ID { get; set; }
    public string Title { get; set; }
    public string Description { get; set; }
    public int Order { get; set; }
    public string Hyperlink { get; set; }
}

public class __invalid_type__1
{
    public int ID { get; set; }
    public string Title { get; set; }
    public string Description { get; set; }
    public int Order { get; set; }
    public string Hyperlink { get; set; }
}

public class __invalid_type__2
{
    public int ID { get; set; }
    public string Title { get; set; }
    public string Description { get; set; }
    public int Order { get; set; }
    public string Hyperlink { get; set; }
}

public class RootObject
{
    public __invalid_type__0 __invalid_name__0 { get; set; }
    public __invalid_type__1 __invalid_name__1 { get; set; }
    public __invalid_type__2 __invalid_name__2 { get; set; }
}

I then cleaned up the class and was left with the following:

public class Articles
{
    public int ID { get; set; }
    public string Title { get; set; }
    public string Description { get; set; }
    public int Order { get; set; }
    public string Hyperlink { get; set; }
}

public class FeaturedArticles
{
    public List<Articles> articles { get; set; }
}

When I attempt to load the data into my singleton for use in the app:

    private void fetchFeaturedArticles()
    {
        var client = new RestClient (_featuredArticlesJsonUrl);
        var request = new RestRequest (Method.GET);
        var response = client.Execute (request);
        _featuredArticles = JsonConvert.DeserializeObject<FeaturedArticles> (response.Content);

        foreach (Articles a in _featuredArticles.Articles)
            Console.WriteLine (a.Title);
    }

I find that the Articles do not get deserialized.

I've verified that the JSON data is returned from the web service. I believe the issue exists in the structure of my JSON feed, where each item returned from the feed is given a name which equals the index the item is being returned as.

I am new to using Json.NET so I'm not sure how I should proceed; I cannot change the structure of the JSON feed but need to consume it's data. Anyone have any recommendations?

like image 888
g0ld2k Avatar asked Mar 20 '23 18:03

g0ld2k


1 Answers

You don't need FeaturedArticles class, you can deserialize the JSON into a Dictionary<string, Articles> like this:

private void fetchFeaturedArticles()
{
    var client = new RestClient (_featuredArticlesJsonUrl);
    var request = new RestRequest (Method.GET);
    var response = client.Execute (request);

    Dictionary<string, Articles> _featuredArticles = JsonConvert.DeserializeObject<Dictionary<string, Articles>>(response.Content);

    foreach (string key in _featuredArticles.Keys)
    {
        Console.WriteLine(_featuredArticles[key].Title);
    }

}

Demo: https://dotnetfiddle.net/ZE1BMl

like image 71
ekad Avatar answered Mar 23 '23 18:03

ekad