0

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
    }
  }
}
1
  • You want to put a password in your JWT? Commented Aug 27, 2018 at 7:09

1 Answer 1

1

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);

Sign up to request clarification or add additional context in comments.

6 Comments

how to access these values? means there are lots of values like 0002,0003 and so on and Password1,Password1 and so on
If you read the sample properly you will find out. The samples provide that information. Easiest way would be to create a data structure and use JsonProperty as described above. Then just deserialize the whole object structure. Also described above.
i want to save '0001' and 'Password' keywords in another variable. how to do that?
Use jobject as described above and access the fields like you would with a dictionary.
I did, read the answer. There is a sample for jobject as well.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.