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.
ShamshielShamshiel
asked Feb 6, 2016 at 6:40
1 Answer 1
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
-
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".Shamshiel– Shamshiel2016年02月06日 09:51:54 +00:00Commented Feb 6, 2016 at 9:51
-
Replace the "var" with "string" then. I've updated my answer.Damien Dennehy– Damien Dennehy2016年02月06日 18:20:30 +00:00Commented Feb 6, 2016 at 18:20
lang-cs