【问题标题】:Cannot deserialize Json object to dictionary无法将 Json 对象反序列化为字典
【发布时间】:2013-06-21 06:10:27
【问题描述】:

我在我的 jquery 文件中创建了一个 JSON 对象

{"showmiddleinitial":"True","showprefix":"True","showsuffix":"True"}

现在我通过解析它使用 AJAX 调用将这个对象发送到我的控制器

$.parseJSON({"showmiddleinitial":"True","showprefix":"True","showsuffix":"True"})

{"showmiddleinitial":"True","showprefix":"True","showsuffix":"True"}

对于上述两种情况,我都会遇到以下错误

Cannot deserialize JSON object into type 'System.Collections.Generic.List`1[System.Collections.Generic.Dictionary`2[System.String,System.String]]'.

Error converting value "{"showmiddleinitial":"True","showprefix":"True","showsuffix":"True"}" to type 'System.Collections.Generic.List`1[System.Collections.Generic.Dictionary`2[System.String,System.String]]'.

谁能引导我走向正确的方向? 我将 ASP.net 与 C#、MVC 和 Jquery 一起使用

我不能在我的 C# 代码中做很多事情,因为它是静态的,因此我只能依赖 jquery。 c# 中的对象是

类型的
List<Dictionary<string, string>> attributesList

嗯...我找到了问题的根源并注意到发送

$.parseJSON([{"showmiddleinitial":"True","showprefix":"True","showsuffix":"True"}])

解决了这个问题。 任何人都可以让我知道如何从 jquery 向 JSON 对象添加方括号,当然无需手动添加它们

【问题讨论】:

标签: c# jquery json serialization


【解决方案1】:

您的 json 字符串值应该用引号括起来,例如

string json = @"{""key1"":""value1"",""key2"":""value2""}";

然后调用

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

【讨论】:

  • 我认为需要方括号 [{"showmiddleinitial":"True","showprefix":"True","showsuffix":"True"}],已解决目的
猜你喜欢
  • 1970-01-01
  • 2019-02-08
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2016-10-08
  • 1970-01-01
相关资源
最近更新 更多