简体   繁体   English

使用json.NET将JSON反序列化为C#dynamic

[英]Deserialize JSON with json.NET into C# dynamic

I have following problem: I have a json file that looks like this 我有以下问题:我有一个看起来像这样的json文件

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

If I parse this JSON-File with Newtonsoft like this 如果我像这样用Newtonsoft解析这个JSON-File

 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? 我怎样才能摆脱Value的东西? All of my objects in the JSON end up being a string. JSON中的所有对象最终都是一个字符串。 I don't want to always write ".Value" at the end if it is not necessary. 如果没有必要,我不想总是在末尾写“.Value”。

I tested this using Newtonsoft 8.0.2 and it works fine. 我使用Newtonsoft 8.0.2测试了它,它工作正常。

        dynamic dyn = JObject.Parse(json);

        string value = dyn.Path.FirstPath;

Value should equal /1/2/text() . 值应等于/1/2/text()

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM