【问题标题】:How to deserialize and get the object and its array keys and values如何反序列化并获取对象及其数组键和值
【发布时间】:2017-07-11 19:41:23
【问题描述】:

我将以下 JSON 分配给变量 strP:

{"get_the_data":[{"when_date":"09/12/2019","which_loc":"Orlando","who_witness":"visitor"}]}

我需要生成以下输出:

get_the_data:
    when_date - 09/12/2019
    which_loc - Orlando
    who_witness - visitor

如何反序列化这个 JSON 以便获取对象中每个数组的 KEY 和 VALUE?到目前为止,这是我尝试过的:

string object1, string array1;
var jsonObj = new JavaScriptSerializer().Deserialize<RO>(strP);
//get the parent key: 'get_the_data'
object1 = get_the_data.ToString();
foreach (var p in strP._data)
{
    //how can I get the KEY and the VALUE of each array within the object
    array1 += p.Key + " - " + p.Value + Environment.NewLine; //e.g. when_date - 09/12/2019
}

Console.WriteLine(object1 + ":" + Environment.NewLine + array1);
//...
public class Data1
{
    public string when_date { get; set; }
    public string which_loc { get; set; }
    public string who_witness { get; set; }
}

public class RO
{
    public List<Data1> _data { get; set; }
}

附言我想避免使用外部 JSON 库并使用本机 C# 方法。

【问题讨论】:

  • 只是出于好奇,您对使用 Json.net 有什么反对意见?它非常成熟,是 Nuget 上下载量排名第一的软件包。通过避免它,您正在为自己创造所以更多的工作
  • 没什么,只是想在本地做...但我会检查一下 :) 谢谢。
  • 我认为对于大多数 .Net 开发人员(甚至在 Microsoft 内部)来说,Json.net 被认为是“本机”json 解决方案。它默认附带大量 Microsoft 模板,尤其是在 asp.net 周围
  • 如果我已经默认使用原生 javascriptdeserializer 并且导入 JSON.net 会破坏什么?
  • 不,它不应该破坏任何东西

标签: c# json deserialization javascriptserializer


【解决方案1】:

如果您只是想从 JSON 中获取键和值而不预先对键名进行硬编码,则可以反序列化为 Dictionary&lt;string, List&lt;Dictionary&lt;string, string&gt;&gt;&gt;:

var jsonObj = new JavaScriptSerializer().Deserialize<Dictionary<string, List<Dictionary<string, string>>>>(strP);

string indent = "   ";
var sb = new StringBuilder();
foreach (var outerPair in jsonObj)
{
    sb.Append(outerPair.Key).AppendLine(":");
    outerPair.Value.SelectMany(d => d).Aggregate(sb, (s, p) => s.Append(indent).Append(p.Key).Append(" - ").AppendLine(p.Value));
}

Console.WriteLine(sb);

顺便说一句,您的 RO 类型不能用于反序列化您的问题中显示的 JSON,因为它的属性名称:

public List<Data1> _data { get; set; }

不同于 JSON 中的属性名称:

{"get_the_data":[ ... ] }

这些属性名称需要匹配,因为JavaScriptSerializer 在(反)序列化期间没有对属性重命名的内置支持。详情请见here。

【讨论】:

  • 我喜欢 LINQ 方法。谢谢。我会测试并告诉你进展如何。
  • 我收到此错误:Type 'System.Collections.Generic.Dictionary2[[System.String, mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b89],[System.Collections.Generic.List@987654331 @2[[System.String,mscorlib,版本=4.0.0.0,Culture=neutral,PublicKeyToken=b89],[System.String,mscorlib,版本=4.0.0.0,Culture=neutral,PublicKeyToken=b89]],mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b89]], mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b89]]' 不支持数组的反序列化。`
  • @Si8 - 如果您的实际 JSON 包含一个数组 - 由 [ 和 ] 包围的有序值序列 - 而不是对象 - 一组无序键,则会发生这种情况由{ 和} 包围的/值对。您可以发布您的实际 JSON 吗?是不是比你展示的 JSON 更复杂?
  • jsfiddle.net/v9o91bm5/1 HTML 是我想从 Json 中检索的内容。键/值都是动态的,所以我想从 Json 本身中提取出来。
  • @Si8 - 我刚刚使用您的新示例运行了答案中的代码,对于输出我得到了samp_data: value_1 - 09/12/2019 value_2 - Leg value_3 - New York value_4 - 0000999201 value_5 - Side value_6 - Test value_7 - 540-090-3443,这就是您想要的。也不例外。如果您可以提供一个不起作用的minimal reproducible example,我可以尝试提供帮助,因为此时问题似乎已得到解答。
猜你喜欢
  • 2015-06-06
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2017-01-31
  • 1970-01-01
  • 1970-01-01
  • 2021-05-30
  • 2023-04-04
相关资源
最近更新 更多