4

How do I get a specific nested property from a JSON using JObject?

For example i want to get the uri:

{
"embed": {
    "uri": "/presets/88930"

...

1
  • myJObj["embed"]["uri"]? Commented May 31, 2017 at 17:36

2 Answers 2

7

There's many ways to access the property you're interested in.

Here's one:

    String jsonData = "{ 'embed': { 'uri': '/presets/88930'}}";
    var jObject = Newtonsoft.Json.Linq.JObject.Parse(jsonData);
    Console.WriteLine((string)jObject["embed"]["uri"]);
Sign up to request clarification or add additional context in comments.

1 Comment

how can we make it exception free. In case if no uri property is there with json , then how we can write this
4

if your jObject looks like:

var j = JObject.Parse(@"{""embed"": { ""uri"": ""/presets/88930"" } }");

dynamics makes accessing the object pretty easy:

string value = ((dynamic)j).embed.uri.ToString();  

Comments

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.