使用json.NET将JSON反序列化为C#动态 [英] Deserialize JSON with json.NET into C# dynamic
本文介绍了使用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()
.
这篇关于使用json.NET将JSON反序列化为C#动态的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文