[英]C# Json Dynamic object
I'm working with a Json dynamic object . 我正在使用Json动态对象 。
Here's what i'm using to get data out of the object: 这是我用来从对象中获取数据的方法:
string = obj.item.today.price;
This works fine, the problem is that as soon as I have to start using numbers example : 这工作正常,问题是我必须开始使用数字示例:
string = obj.daily.10000;
It gives me an error 它给了我一个错误
Is there any way to fix this? 有没有什么办法解决这一问题?
That's not possible to " call
10000 on daily object
" just because 10000
is NOT a valid identifier . 因为10000
不是有效的标识符,所以不能“ 10000 on daily object
call
10000 on daily object
”。
Let me explain what is going on here: 让我解释一下这里发生了什么:
JSON
parser generates some runtime type, inherited from some base JSON
type (eg JsonObject
). JSON
解析器生成一些运行时类型,继承自一些基本JSON
类型(例如JsonObject
)。 So, obj
is some generated type, you call property item
on it, it returns similar generated type, then you call today
property and so on. 所以, obj
是一些生成类型,你在它上面调用属性item
,它返回类似的生成类型,然后你调用today
属性等等。
The last step is weird, there cannot be 10000
property on any type, generated or not. 最后一步很奇怪,生成或不生成任何类型的10000
属性。
But, If library supports key-value access to objects , you can try to write 但是, 如果库支持对对象的键值访问 ,则可以尝试编写
obj.daily["10000"]
or cast obj to JObject (assume you are using JSON.NET) and call Property method: 或者将obj 强制转换为JObject (假设您使用的是JSON.NET)并调用Property方法:
var jsonObject = (JObject) obj;
var propertyValue = jsonObject.Property("10000").Value;
If you using Json.NET 如果您使用Json.NET
string json = "{ dayly : { 1000 : 'asd' } }";
dynamic d = JsonConvert.DeserializeObject(json);
Console.WriteLine((d.dayly as JObject).Property("1000").Value);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.