Question

Good day,

I am having with parsing JSON response. Let's say I have this JSON:

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

Okay. So what if I want to just display the datas like "dataInfo" only..in Python, I can easily do it by doing this:

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

That would display this:

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

How can I do it in C#? I tried this: http://procbits.com/2011/08/11/fridaythe13th-the-best-json-parser-for-silverlight-and-net

But that only worked for non-array JSON..

Hope someone can guide me,

Was it helpful?

Solution

If you use Json.NET

JObject obj = JObject.Parse(File.ReadAllText("1.json"));
foreach (JToken o in obj["data"]["innerData"] as JArray)
    Console.WriteLine(o["dataInfo"]);
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top