C#如何在动态对象中获取nth对象属性名称

发布于 2025-02-13 14:52:04 字数 1116 浏览 3 评论 0原文

我试图列举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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

愿得七秒忆 2025-02-20 14:52:04

您可以按照查理(Charlie)的评论进行操作,但是如果您需要迭代属性,这是一种方法。

public void DoWork()
        {
            string jsonString = @"
{
""order_type"": ""request"",
""order_id"": ""A511"",
""order_Date"" : null,
""order_name"": ""Preston"",
}
";
            JObject jObject = JObject.Parse(jsonString);
            foreach (KeyValuePair<string, JToken> pair in jObject)
            {
                string keyName = pair.Key;
                Console.WriteLine(keyName);
            }

        }

You can do the Where as Charlie's comment mentions, but if you need to iterate properties, this is one way.

public void DoWork()
        {
            string jsonString = @"
{
""order_type"": ""request"",
""order_id"": ""A511"",
""order_Date"" : null,
""order_name"": ""Preston"",
}
";
            JObject jObject = JObject.Parse(jsonString);
            foreach (KeyValuePair<string, JToken> pair in jObject)
            {
                string keyName = pair.Key;
                Console.WriteLine(keyName);
            }

        }
猫七 2025-02-20 14:52:04

对于开瓶器,我确实弄清楚了这一点:

TypeDescriptor.GetProperties(data.elements[i])[2].Name; // --> "order_date"

所以,现在可以()每个人都可以检查空。

向其他/更好的解决方案开放。

For openers, I did figure out this:

TypeDescriptor.GetProperties(data.elements[i])[2].Name; // --> "order_date"

So, now I can for() each one and check for nulls.

Open to other/better solutions.

若水微香 2025-02-20 14:52:04

只需在属性上使用linq

x.Properties.Where(j => j.Type == JTokenType.Null)

Just use LINQ on the Properties

x.Properties.Where(j => j.Type == JTokenType.Null)
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文