C# – 无法反序列化当前的JSON数组时出错

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

C# - Error where cannot deserialize the current JSON array

问题

  1. 我想显示州和城市的列表。
  2. 以下是我做的州和城市列表:
  3. ```json
  4. [
  5. { "State": "California",
  6. "City":
  7. ["Los Angeles",
  8. "San Diego",
  9. "San Francisco"
  10. ]
  11. },
  12. { "State": "Texas",
  13. "City":
  14. ["Houston",
  15. "San Antonio",
  16. "Dallas"
  17. ]
  18. }
  19. ]

这是我的代码:

  1. List<string> stateCities = new List<string>();
  2. var dictionary = stateCities.Select(x => JsonConvert.DeserializeObject<Dictionary<string, object>>(x))
  3. .ToDictionary(x => x["State"].ToString(), x => ((JArray)x["City"]).ToObject<List<string>>());
  4. lookUpEditState.Properties.DataSource = dictionary;
  5. lookUpEditState.Properties.ValueMember = "State";
  6. lookUpEditCity.Properties.DataSource = dictionary;
  7. lookUpEditCity.Properties.ValueMember = "City";
  8. lookUpEditCity.CascadingOwner = lookUpEditState;

我得到了这个错误:

设置失败:无法将当前的 JSON 数组(例如 [1,2,3])反序列化为类型 'System.Collections.Generic.Dictionary`2[System.String,System.Object]',因为该类型需要一个 JSON 对象(例如 {"name":"value"})才能正确反序列化。
要解决此错误,要么将 JSON 更改为 JSON 对象(例如 {"name":"value"}),要么将反序列化类型更改为数组或实现集合接口的类型(例如 ICollection、IList),如可从 JSON 数组反序列化的 List
也可以向类型添加 JsonArrayAttribute,强制它从 JSON 数组反序列化。路径 '', 第1行,第1个位置。

有什么办法可以解决这个问题吗?谢谢

  1. <details>
  2. <summary>英文:</summary>
  3. I want to display the list of state and city.
  4. The following is the list for state and city that I&#39;ve done:
  5. ```json
  6. [
  7. { &quot;State&quot;: &quot;California&quot;,
  8. &quot;City&quot;:
  9. [&quot;Los Angeles&quot;,
  10. &quot;San Diego&quot;,
  11. &quot;San Francisco&quot;
  12. ]
  13. },
  14. { &quot;State&quot;: &quot;Texas&quot;,
  15. &quot;City&quot;:
  16. [&quot;Houston&quot;,
  17. &quot;San Antonio&quot;,
  18. &quot;Dallas&quot;
  19. ]
  20. }
  21. ]

Here is my code

  1. List&lt;string&gt; stateCities = new List&lt;string&gt;();
  2. var dictionary = stateCities.Select(x =&gt; JsonConvert.DeserializeObject&lt;Dictionary&lt;string, object&gt;&gt;(x))
  3. .ToDictionary(x =&gt; x[&quot;State&quot;].ToString(), x =&gt; ((JArray)x[&quot;City&quot;]).ToObject&lt;List&lt;string&gt;&gt;());
  4. lookUpEditState.Properties.DataSource = dictionary;
  5. lookUpEditState.Properties.ValueMember = &quot;State&quot;;
  6. lookUpEditCity.Properties.DataSource = dictionary;
  7. lookUpEditCity.Properties.ValueMember = &quot;City&quot;;
  8. lookUpEditCity.CascadingOwner = lookUpEditState;
  9. }

i got this error:

> Fail to setup: Cannot deserialize the current JSON array (e.g. [1,2,3]) into type
'System.Collections.Generic.Dictionary`2[System.String,System.Object]' because the type requires a JSON object (e.g. {"name":"value"}) to deserialize correctly.
To fix this error either change the JSON to a JSON object (e.g. {"name":"value"}) or change the deserialized type to an array or a type that implements a collection interface (e.g. ICollection, IList) like List<T> that can be deserialized from a JSON array.
JsonArrayAttribute can also be added to the type to force it to deserialize from a JSON array. Path '', line 1, position 1.

Any idea how can i fix this? Thank you

答案1

得分: 2

你需要使用与其表示匹配的类来反序列化JSON。在这种情况下,你有一个具有State和City属性的对象数组,所以你可以这样使用:

  1. var template = new [] { new { State = "", City = new string[] {} }};
  2. var array = JsonConvert.DeserializeAnonymousType(jsonText, template);

如果你真的想要,然后你可以将其转换为字典:

  1. var dictionary = array.ToDictionary(x => x.State, y => y.City);

...这将给你一个 Dictionary<string, string[]>

英文:

You need to deserialize JSON using a class that matches its representation. In this case you have an array of objects that have a State and City property, so you could use this:

  1. var template = new [] { new { State = &quot;&quot;, City = new string[] {} }};
  2. var array = JsonConvert.DeserializeAnonymousType(jsonText, template);

You could then convert it to a dictionary if you really want:

  1. var dictionary = array.ToDictionary( x =&gt; x.State, y =&gt; y.City );

...which will give you a Dictionary&lt;string,string[]&gt;.

答案2

得分: 1

这对我有效

  1. Dictionary&lt;string, List&lt;string&gt;&gt; dictionary = JArray.Parse(json)
  2. .ToDictionary(ja =&gt; (string)ja[&quot;State&quot;],ja =&gt; ja[&quot;City&quot;]
  3. .Select(i =&gt; (string)i).ToList());
英文:

This works for me

  1. Dictionary&lt;string, List&lt;string&gt;&gt; dictionary = JArray.Parse(json)
  2. .ToDictionary(ja =&gt; (string)ja[&quot;State&quot;],ja =&gt; ja[&quot;City&quot;]
  3. .Select(i =&gt; (string)i).ToList());
  4. </details>

huangapple
  • 本文由 发表于 2023年6月16日 10:01:20
  • 转载请务必保留本文链接:https://go.coder-hub.com/76486527.html
匿名

发表评论

匿名网友

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

确定