4

An API is returning to me the following json:

{
   "query":{
      "pages":{
         "49123":{
            "pageid":49123,
            "ns":0,
            "title":"Phoenix (constellation)",
            "revisions":[
               {
                  "revid":588710862,
                  "parentid":588710834
               }
            ]
         }
      }
   }
}

I used json2csharp to build a class representing this json (I manually tweaked the names for PageInfo, as it was coughing on this name and calling it __invalid_type__49123)

public class Revision
{
    public int revid { get; set; }
    public int parentid { get; set; }
}

public class PageInfo
{
    public int pageid { get; set; }
    public int ns { get; set; }
    public string title { get; set; }
    public List<Revision> revisions { get; set; }
}

public class Pages
{
    public PageInfo pageInfo { get; set; }
}

public class Query
{
    public Pages pages { get; set; }
}

public class RootObject
{
    public Query query { get; set; }
}

and tried to parse it:

        var json = "{\"query\":{\"pages\":{\"49123\":{\"pageid\":49123,\"ns\":0,\"title\":\"Phoenix (constellation)\",\"revisions\":[{\"revid\":588710862,\"parentid\":588710834}]}}}}";
        // unescaped version of json below
        // {"query":{"pages":{"49123":{"pageid":49123,"ns":0,"title":"Phoenix (constellation)","revisions":[{"revid":588710862,"parentid":588710834}]}}}}

        var root = JsonConvert.DeserializeObject<RootObject>(json);

I can examine and see root.query.pages all have values, but pageInfo is null. I'm not sure what I'm missing that will allow me to load this json into an object.

1
  • It's because 49123 is not mapping to PageInfo property inside the Pages class. This is an interesting situation here because it looks like that particular element in the JSON is the value of the PageId so it will be different for each instance of your JSON string. If it was a constant, you could've used [JsonProperty(PropertyName = "Myname")] decoration on the PageInfo property in the Pages class. Commented Jan 1, 2014 at 21:29

1 Answer 1

6

Change your class definiton as below. it will work.. (see the definition of Query class)

public class Revision
{
    public int revid { get; set; }
    public int parentid { get; set; }
}

public class Page
{
    public int pageid { get; set; }
    public int ns { get; set; }
    public string title { get; set; }
    public List<Revision> revisions { get; set; }
}

public class Query
{
    public Dictionary<string,Page>  pages { get; set; }
}

public class RootObject
{
    public Query query { get; set; }
}
Sign up to request clarification or add additional context in comments.

3 Comments

+1 for the right answer. There's was typo in your answer. Query class definition should be as follows.public class Query { public Dictionary<string, PageInfo> pages { get; set; } } . I updated your answer. I tested this and it works.
@Shiva No, there is no PageInfo in my code. I rolled back your edit.
Oh, just saw that you changed OP's PageInfo class to Page class. I'd copied OP's code and added the Query class from your code, not noticing that you'd renamed the PageInfo class. My bad.

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.