cayman187
cayman187

Reputation: 33

Json to c# get child nodes

I am getting a response from a API that returns the following json:

[
  {
   "input_index":0,
   "candidate_index":0,
   "components":,
      {
       "child1":"sam",
       "child2":"tom",
      }
  }
 ]

i need to get the results of child1 and child2 using c# and json.net.

Upvotes: 0

Views: 12446

Answers (1)

AlliterativeAlice
AlliterativeAlice

Reputation: 12577

It should be fairly straightforward. Just parse the JSON, then get the child items from the components object of the array's first index:

var obj = JArray.Parse(json);
string child1 = (string)obj[0]["components"]["child1"];
string child2 = (string)obj[0]["components"]["child2"];

Upvotes: 5

Related Questions