简体   繁体   English

无法解析具有复杂对象的JSON(JSON.Net)

[英]Unable to parse json with complex object (JSON.Net)

I am unable to parse below json using Json.net library. 我无法使用Json.net库解析json以下的内容。 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. 我正在使用JSON.net库,并且能够获取“列表”对象以外的数据。 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 API提供程序的JSON结构

Below is Json Structure from API provider. 以下是来自API提供程序的Json Structure。

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

I think the problem is that list is not an array. 我认为问题在于列表不是数组。 If so the json would look like this: 如果是这样,则json如下所示:

{"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: 编辑:“列表”的使用令人困惑,因为它仍然不是一个jsonarray,新的json等效于:

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. 您提供的字符串不是有效的Json,因为缺少逗号等。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM