使用json.NET将JSON反序列化为C#动态
问题描述:
我有以下问题:我有一个看起来像这样的json文件
I have following problem: I have a json file that looks like this
{
"Path": {
"FirstPath": "/1/2/text()"
}
}
如果我像这样用Newtonsoft解析此JSON文件
If I parse this JSON-File with Newtonsoft like this
dynamic dyn = JObject.Parse(json);
或这个
dynamic dyn = JsonConvert.DeserializeObject(json);
我得到一个需要像这样使用的动态对象
I get a dynamic object that needs to be used like this
dyn.Path.FirstPath.Value
我如何摆脱价值"的东西?我在JSON中的所有对象最终都是一个字符串.如果不需要,我不想总是在最后写".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.
答
我使用Newtonsoft 8.0.2对此进行了测试,并且效果很好.
I tested this using Newtonsoft 8.0.2 and it works fine.
dynamic dyn = JObject.Parse(json);
string value = dyn.Path.FirstPath;
值应等于/1/2/text()
.