8

I have following problem: I have a json file that looks like this

{
    "Path": {
        "FirstPath": "/1/2/text()"
    }
}

If I parse this JSON-File with Newtonsoft like this

 dynamic dyn = JObject.Parse(json);

or this

dynamic dyn = JsonConvert.DeserializeObject(json);

I get a dynamic object that needs to be used like this

dyn.Path.FirstPath.Value

How can I get rid of the Value stuff? All of my objects in the JSON end up being a string. I don't want to always write ".Value" at the end if it is not necessary.

1 Answer 1

12

I tested this using Newtonsoft 8.0.2 and it works fine.

        dynamic dyn = JObject.Parse(json);

        string value = dyn.Path.FirstPath;

Value should equal /1/2/text().

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

2 Comments

Yes but if you look closer you can see that "value" isn't a string it is from Type "Newtonsoft.Json.Linq.JValue". If you take "value" and pass it to a Method that expects a string you will get an Exception. It only works if you write "value.Value".
Replace the "var" with "string" then. I've updated my answer.

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.