【问题标题】:Possible JSON to Dictionary in C#C# 中可能的 JSON 到字典
【发布时间】:2015-04-21 08:38:28
【问题描述】:

我对@9​​87654322@s 的概念有点新鲜,所以请耐心等待。

我有一个方法返回以下长而复杂的字符串:

[{\"identifierType\":\".Identifier\",\"title\":\"Test\",\"typeName\":\"Application1\",\"key\":\"1\",\"internalId\":1},{\"identifierType\":\".Identifier\",\"title\":\"Test2\",\"typeName\":\"Application2\",\"key\":\"2\",\"internalId\":15},{\"identifierType\":\".Identifier\",\"title\":\"Test3\",\"typeName\":\"Application3\",\"key\":\"3\",\"internalId\":1124},{\"identifierType\":\".Identifier\",\"title\":\"Test4\",\"typeName\":\"Application4\",\"key\":\"4\",\"internalId\":2241}]

她长而丑陋的眼睛,但这是我必须解决的问题。

经过一番谷歌搜索,我发现这是一个JSON-string,并且她可以映射到C#中的Dictionary

现在,经过一番谷歌搜索后,我得到了这个:

Dictionary<string, string> values = JsonConvert.DeserializeObject<Dictionary<string, string>>(s);

在这种情况下,s 是我的又长又丑的字符串。

问题是这样运行时会报错。

无法将当前 JSON 数组(例如 [1,2,3])反序列化为类型 'System.Collections.Generic.Dictionary`2[System.String,System.String]' 因为该类型需要一个 JSON 对象(例如 {"name":"value"}) 正确反序列化。

要修复此错误,请将 JSON 更改为 JSON 对象(例如 {"name":"value"}) 或将反序列化类型更改为数组或 实现集合接口的类型(例如 ICollection、IList) 比如可以从 JSON 数组反序列化的 List。 JsonArrayAttribute 也可以添加到类型中以强制它 从 JSON 数组反序列化。

路径'',第 1 行,位置 1。

现在我在JSON 里像白菜一样绿,我被这弄得目瞪口呆。

我不那么漂亮的s不是我认为的JSON字符串吗?

如果她是,那么理论上这不应该有效吗?

我还预见到s 中似乎有多个对象这一事实存在问题。当我尝试将其映射到 Dictionary 时,这不会造成麻烦吗?

我是否必须将s 映射到多个Dictionarys?

请指教。

【问题讨论】:

  • 你试过List&lt;Dictionary&lt;string, string&gt;&gt; values = JsonConvert.DeserializeObject&lt;List&lt;Dictionary&lt;string, string&gt;&gt;&gt;(s);吗?因为您的 JSON 文本以 [ 开头
  • 这些转义引号实际上在 json 中吗?
  • 你的 json 似乎无效,在这里测试一下,它给出了无效的 json codebeautify.org/jsonvalidate
  • 它是有效的,它只是作为 c# 字符串文字转义。将所有\"s 替换为"s :)
  • 尝试去掉字符串开头和结尾的方括号,因为它会从您的字符串中生成数组,但是反序列化器无法正确识别它。您的源字符串中有 4 个项目,由逗号分隔,因此要正确反序列化它,您需要创建 List od Dictionaries。

标签: c# json dictionary


【解决方案1】:

您的 json 是一个数组,假设为 YourJsonObject,因此您可以将其反序列化为 List&lt;YourJsonObject&gt;

http://json2csharp.com 站点在这些场景中派上用场,因此通过将您的 json 粘贴到那里,它会发出以下类:

public class YourJsonObject
{
    public string identifierType { get; set; }
    public string title { get; set; }
    public string typeName { get; set; }
    public string key { get; set; }
    public int internalId { get; set; }
}

var list = JsonConvert.Deserialize&lt;List&lt;YourJsonOject&gt;&gt;(s);

得到你想要的东西。如果您真的想要,可以按照我上面的评论者的建议将其反序列化为字典列表,但由于它也包含一个整数,因此这种方式可能会更好。

【讨论】:

  • 完美!正是我需要的! json2csharp.com 是多么方便的工具啊。朋友,+1 和最佳答案!
【解决方案2】:

首先,让我们美化您的 JSON。我使用了JsonEditorOnline 的往返函数,该网站对于一次性调查 JSON 数据非常有用:

[
  {
    "identifierType": ".Identifier",
    "title": "Test",
    "typeName": "Application1",
    "key": "1",
    "internalId": 1
  },
  {
    "identifierType": ".Identifier",
    "title": "Test2",
    "typeName": "Application2",
    "key": "2",
    "internalId": 15
  },
  {
    "identifierType": ".Identifier",
    "title": "Test3",
    "typeName": "Application3",
    "key": "3",
    "internalId": 1124
  },
  {
    "identifierType": ".Identifier",
    "title": "Test4",
    "typeName": "Application4",
    "key": "4",
    "internalId": 2241
  }
]

JSON 有一个很好的特性,即如果您了解基础知识,那么人类就很容易阅读。方括号[ ] 表示数组,大括号{ } 表示对象。
在这种情况下,您有一个对象数组,其属性为identifierTypetitletypeNamekeyinternalId

将其转换为可在 C# 中使用的数据的最简单方法是创建一个与此结构匹配的类:

public class MyItem // pick a better name, of course
{
    [JsonProperty("identifierType")]
    public string IdentifierType { get; set; }

    // add the other properties in the same fashion
}

然后,您可以将该 JSON 转换为您的数据结构:

var items = JsonConvert.DeserializeObject<List<MyItem>>( yourJsonString );

【讨论】:

  • 大量 +1 以获得出色的答案。我发现下面的那个更容易理解,但如果我能给你+10,我会的!
【解决方案3】:

你的 's' 不是 json。

它的 Json 对象的 Json 数组。 用 C# 术语说字典列表。

例如-

JsonArray:

[{"d":1},{"y":1},{"z":3}]

解决方案可以解析成json数组,然后foreach值创建一个字典。

Json 对象:

{"a":1,"b":2}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2021-05-23
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-04-20
    • 1970-01-01
    相关资源
    最近更新 更多