C#解析JSON数组(HTTPrequest Response)以显示

时间:2013-07-11 13:21:39

标签: c# json python-3.x json-deserialization

美好的一天,

我正在解析JSON响应。假设我有这个JSON:

{
   "data": {
       "count" : 3,
       "innerData" : [
       {
           "dataInfo" : "heheh",
           "dataInfo2" : "hahah",
           "dataInfo3" : "huhuh"
       },
               {
           "dataInfo" : "jejej",
           "dataInfo2" : "jajaj",
           "dataInfo3" : "jujuj"
       },
               {
           "dataInfo" : "fefef",
           "dataInfo2" : "fafaf",
           "dataInfo3" : "fufuf"
       }
       ]
   }
}

好。那么,如果我只想在Python中显示“ dataInfo ”这样的数据,我可以通过这样做轻松实现:

for x in response.json()['data']['innerData']
    print(x['dataInfo'])

这会显示:

>>> heheh
>>> jejej
>>> fefef

我怎样才能在C#中做到这一点?我试过这个:http://procbits.com/2011/08/11/fridaythe13th-the-best-json-parser-for-silverlight-and-net

但这只适用于非数组JSON ..

希望有人可以指导我,

1 个答案:

答案 0 :(得分:0)

如果您使用Json.NET

JObject obj = JObject.Parse(File.ReadAllText("1.json"));
foreach (JToken o in obj["data"]["innerData"] as JArray)
    Console.WriteLine(o["dataInfo"]);