无法使用复杂对象解析json(JSON.Net)

时间:2012-10-29 06:56:07

标签: c# .net json windows-phone-7 json.net

我无法使用Json.net库在json下解析。如果对象进入对象内,我会混淆该做什么。 我正在使用JSON.net库并且能够获取除“list”对象之外的数据。请帮忙。

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

请检查我的以下代码

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

错误说明

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",

API提供商的JSON结构

以下是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
      }
   }
}   

1 个答案:

答案 0 :(得分:1)

我认为问题是列表不是数组。如果是这样,json看起来像这样:

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

你可以试试这个:

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

编辑: 使用“list”令人困惑,因为它仍然不是jsonarray 新的json相当于:

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

你可以试试这个:

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

你提供的字符串不是有效的Json,因为有逗号丢失等等。