Deserialize JSON with json.NET into C# dynamic

橙三吉。 提交于 2019-12-21 11:02:19

问题


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:


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



来源:https://stackoverflow.com/questions/35238194/deserialize-json-with-json-net-into-c-sharp-dynamic

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!