0

I am unable to parse below json using Json.net library. I am confuse what to do in case object comes within object. I am using JSON.net library and able to get data except "list" object. Please help.

            @"{""status"":1, ""list"": 
                {""231784875"": 
                    {
                        ""item_id"":""231784875"",
                        ""title"":""ASP.Net Skill Test, ASP.Net quiz, ASP.Net Online Tests, Online Assessments,"",
                        ""url"":""http:\/\/www.techgig.com\/skilltest\/ASP-Net"",
                        ""time_updated"":""1351228692"",
                        ""time_added"":""1349344004"",
                        ""state"":""1""
                    }
                }
            ,""since"":1351228692,
            ""complete"":0
            }";

Please check my below codes

private void ReadWebRequestCallback(IAsyncResult callbackResult)
    {
        HttpWebRequest myRequest = (HttpWebRequest)callbackResult.AsyncState;
        HttpWebResponse myResponse = (HttpWebResponse)myRequest.EndGetResponse(callbackResult);

        using (StreamReader httpwebStreamReader = new StreamReader(myResponse.GetResponseStream()))
        {
            string results = httpwebStreamReader.ReadToEnd();
            JObject o= JObject.Parse(results);
            JArray list = (JArray) o[o["list"]];
            //getting error 
        }
        myResponse.Close();
    }

Error description

Accessed JObject values with invalid key value: {
  "211384805": {
    "item_id": "211384805",
    "title": "Introduction | Developer Portal",
    "url": "https://developer.uidai.gov.in/site/node/19",
    "time_updated": "1351109730",

JSON Structure by API Provider

Below is Json Structure from API provider.

{
   "status":"1",            // 1=normal, 2=no changes since your provided 'since'
   "since":"1245626956',        // timestamp of this response
   "list":{
      "93817":{
         "item_id":"93817"          // unique id identifying the url
         "url":"http://url.com",
         "title":"Page Title",
         "time_updated":"1245626956",       // time the item was last added/changed
         "time_added":"1245626956",     // time item was added to list
         "tags":"comma,seperated,list",
         "state":"0",                       // 0=unread, 1=read
      },
      "935812":{
         "item_id":"935812"         // unique id identifying the url
         "url":"http://google.com",
         "title":"Google",
         "time_updated":"1245626956",       // time the item was last added/changed
         "time_added":"1245626956",     // time item was added to list
         "tags":"comma,seperated,list",
         "state":"1",                       // 0=unread, 1=read
      }
   }
}   
2
  • when i have to deserialize json i use newtonsoft, and it's pretty easy james.newtonking.com/pages/json-net.aspx Commented Oct 29, 2012 at 7:53
  • I am also using newtonsoft but not sure how to map anonymous object. If you see in error description "211384805: is coming. I want to know how to handle this value. This value is dynamic and can change with every item. Commented Oct 29, 2012 at 8:11

1 Answer 1

1

I think the problem is that list is not an array. If so the json would look like this:

{"list":[
            {...}
        ]
}

You can try this:

JObject o = JObject.Parse(json);
JArray jArray;
if(o["list"].Type==JTokenType.Array)
{
    jArray = (JArray) o["list"];
}

Edit: The use of "list" is confusing since it is still not a jsonarray the new json is equivalent to:

public class List
{
    public 93817 { get; set; }
    public 935812 { get; set; }
}

You can try this:

JObject jObject = JObject.Parse(json);
var array = new JArray(jObject["list"].Values());

The string you provided is not valid Json by the way there were commas missing etc.

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

2 Comments

I just added structure which API provider is giving. Please advice.
Thanks Larsson. I appreciate your help. Let me try this first.

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.