【问题标题】:C# Parsing JSON Array(HTTPrequest Response) to displayC# 解析 JSON 数组(HTTPrequest 响应)以显示
【发布时间】:2013-07-11 13:21:38
【问题描述】:

早安,

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

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

好的。那么如果我只想显示像“dataInfo”这样的数据怎么办..在Python中,我可以很容易地做到这一点:

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..

希望有人可以指导我,

【问题讨论】:

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


    【解决方案1】:

    如果你使用 Json.NET

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

    【讨论】:

    • 谢谢,这对我有用!
    猜你喜欢
    • 1970-01-01
    • 2011-05-20
    • 1970-01-01
    • 2021-11-13
    • 2021-08-15
    • 2020-02-28
    • 1970-01-01
    • 1970-01-01
    • 2023-03-08
    相关资源
    最近更新 更多