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.

asked Feb 6, 2016 at 6:40

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().

answered Feb 6, 2016 at 8:36
2
  • 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". Commented Feb 6, 2016 at 9:51
  • Replace the "var" with "string" then. I've updated my answer. Commented Feb 6, 2016 at 18:20

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

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.