Deepak Tomar
Deepak Tomar

Reputation: 57

How to parse such type of JSON (Nested) in C#

I want to parse such type of json in C#. how to do that if i want to parse "edit", "0001", "Password" and value of "hasParent"?

{
  "edit": {
    "0001": {
      "PKey": [
        "Password"
      ],
      "hasParent": 0
    }
  }
}

Upvotes: 0

Views: 58

Answers (1)

alsami
alsami

Reputation: 9815

Create a JObject from that JSON and access the values like you would with a dictionary. For instance like that

var jObject = JObject.Parse(json);
var innerJObject = JObject.Parse(jObject["0001"]); // there are better ways to do it, just check out the newtonsoft docs

You can also create an object structure and use data annotation

public class MyClass
{
   [JsonProperty("edit")]
   public MySubClass Subclass { get; set; }
   // ... more properties
}

Then just go ahead and use JsonConvert.DeserializeObject<MyClass>(json);

Upvotes: 1

Related Questions