C# 中列出 JSonObject 或 JSonNode 中的字段名称

huangapple go评论65阅读模式
英文:

C# List field names in a JSonObject or JSonNode

问题

I can help you with the translation. Here's the translated text:

我需要处理各种优先级的JSON数组。它们唯一的共同点是它们都是JSON对象的数组,而每个对象都是一个字典。不幸的是,不是所有字典值都是字符串。有些是数字、日期或布尔值。

所以我的问题是:给定一个JSonObject或JsonNode,如何列出它的所有字段,以便我可以遍历它们并将它们转换为字符串。

重要的是要记住,我事先不知道字段的名称或类型。所以我不能为它们定义类。

我的理想输出是一个列表<字典<string,string>>。

在这一点上,我想在这里放出我尝试过的内容,但不幸的是,我还没有找到符合我的要求的方法。

我尝试过这个:

 JsonArray list = node.AsArray();
 for (int i = 0; i < list.Count; i++){
    
    JsonObject row = list[i].AsObject();
    string jstring = row.ToJsonString();
    Console.WriteLine(jstring);
    Dictionary<string,string> row_data = JsonSerializer.Deserialize<Dictionary<string,string>>(jstring);
    Console.WriteLine(row_data);

 }

其中node是函数的输入。但是,如预期的那样,它没有起作用,因为生成的jstring中并不是所有值都包含“”,所以当重新解释为字典时,它不能工作,因为它们不是字符串。

英文:

I need to process JSON arrays of varied precedence. The only thing they have in common is that the are array of JSON objects and that each object is a dictionary. Unfortunately, not all dictionary values are strings. Some are numbers, dates or boolean.

So my question is: Given a JSonObject or JsonNode how can list all it's fields so that I may then iterate over them and cast them as stirng.

It is importnat to remember that I don't know the fields name or type before hand. So I can't be defining classes for them.

My ideal output is a List < Dictionary<string,string> >

At this point I would put here what I have tried but unfortunatly I haven't found anything that does what I want.

I did try this:

 JsonArray list = node.AsArray();
 for (int i = 0; i &lt; list.Count; i++){
    
    JsonObject row = list[i].AsObject();
    string jstring = row.ToJsonString();
    Console.WriteLine(jstring);
    Dictionary&lt;string,string&gt; row_data = JsonSerializer.Deserialize&lt;Dictionary&lt;string,string&gt;&gt;(jstring);
    Console.WriteLine(row_data);

 }

Where node is the input to the function. But, as expected it did not work, as not all alues in the generated jstring contained " so when reinterpreting back to a dictionary it couldn't becuase they weren't strings.

答案1

得分: 0

请注意,以下是翻译好的部分:

"Instead of using a Dictionary&lt;string, string&gt; use a Dictionary&lt;string, object&gt; and then convert all the values that now are represented as object to strings after deserializing. Ideally you would know exactly what name/value types you can expect and define a type that represents that object but sometimes if you just don't know it's better to be flexible and use this type of dictionary."

请告诉我如果您需要任何进一步的翻译或帮助。

英文:

Instead of using a Dictionary&lt;string, string&gt; use a Dictionary&lt;string, object&gt; and then convert all the values that now are represented as object to strings after deserializing. Ideally you would know exactly what name/value types you can expect and define a type that represents that object but sometimes if you just don't know it's better to be flexible and use this type of dictionary.

huangapple
  • 本文由 发表于 2023年4月17日 21:54:06
  • 转载请务必保留本文链接:https://go.coder-hub.com/76035924.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定