Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Deserialize JSON with json.NET into C# dynamic

Tags:

json

c#

json.net

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.

like image 539
Shamshiel Avatar asked Feb 06 '16 06:02

Shamshiel


1 Answers

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

like image 151
Damien Dennehy Avatar answered Oct 22 '22 21:10

Damien Dennehy