C#如何在动态对象中获取nth对象属性名称
我试图列举JSON避难所的属性名称。
dynamic x = data.elements[i];
Console.Write(x);
{{
"order_type": "request",
"order_id": "A511",
"order_Date" : null,
"order_name": "Preston",
}}
.getProperties()将返回以下内容:
Console.Write(x.GetType().GetProperties())
[0]: {Newtonsoft.Json.Linq.JTokenType Type}
[1]: {Newtonsoft.Json.Linq.JToken Item [System.Object]}
[2]: {Newtonsoft.Json.Linq.JToken Item [System.String]}
[3]: {Boolean HasValues}
[4]: {Newtonsoft.Json.Linq.JToken First}
[5]: {Newtonsoft.Json.Linq.JToken Last}
[6]: {Int32 Count}
[7]: {Newtonsoft.Json.Linq.JContainer Parent}
[8]: {Newtonsoft.Json.Linq.JToken Root}
[9]: {Newtonsoft.Json.Linq.JToken Next}
[10]: {Newtonsoft.Json.Linq.JToken Previous}
[11]: {System.String Path}
它具有“ first
”和“ last
”。它具有“ Next
”和“ 上一个
”。它甚至具有“ parent
”。
但是我需要“ nth”属性的名称。例如,我需要对“ order_date”的引用才能获取其名称。
这里的目标是用空字符串替换为空的“”。但是我需要一种确定属性名称的方法的方法。
如果我完全是错误的,并且有更好的方法可以做到这一点,那么我就可以进行更正。
I am trying to enumerate the NAMES of properties of JSON deserialised object.
dynamic x = data.elements[i];
Console.Write(x);
{{
"order_type": "request",
"order_id": "A511",
"order_Date" : null,
"order_name": "Preston",
}}
.GetProperties() will return the following:
Console.Write(x.GetType().GetProperties())
[0]: {Newtonsoft.Json.Linq.JTokenType Type}
[1]: {Newtonsoft.Json.Linq.JToken Item [System.Object]}
[2]: {Newtonsoft.Json.Linq.JToken Item [System.String]}
[3]: {Boolean HasValues}
[4]: {Newtonsoft.Json.Linq.JToken First}
[5]: {Newtonsoft.Json.Linq.JToken Last}
[6]: {Int32 Count}
[7]: {Newtonsoft.Json.Linq.JContainer Parent}
[8]: {Newtonsoft.Json.Linq.JToken Root}
[9]: {Newtonsoft.Json.Linq.JToken Next}
[10]: {Newtonsoft.Json.Linq.JToken Previous}
[11]: {System.String Path}
It has "First
", and "Last
". It has "Next
" and "Previous
". It even has "Parent
".
But I need the name of the "nth" property. For instance, I need a reference to "order_date" in order to get it's name.
The goal here is to replace the null with empty string "". But I need a way of determining the name of the property that has that null.
If I am going about this completely wrong, and there is a better way to do this, I am open to correction.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
您可以按照查理(Charlie)的评论进行操作,但是如果您需要迭代属性,这是一种方法。
You can do the Where as Charlie's comment mentions, but if you need to iterate properties, this is one way.
对于开瓶器,我确实弄清楚了这一点:
所以,现在可以()每个人都可以检查空。
向其他/更好的解决方案开放。
For openers, I did figure out this:
So, now I can for() each one and check for nulls.
Open to other/better solutions.
只需在
属性上使用linq
Just use LINQ on the
Properties