【发布时间】:2019-03-06 09:58:36
【问题描述】:
将 json 字符串反序列化为对象时遇到问题。 主要问题是我无法识别这个字符串代表什么类型的对象:
string jsonDataText = @"{""sun"":""heat"", ""planet"":""rock"", ""earth"":""water"", ""galaxy"":""spiral""}";
它看起来像 KeyValuePair 对象列表,但是当我尝试使用 Newtonsoft.Json 进行反序列化时:
var clone = JsonConvert.DeserializeObject<List<KeyValuePair<string,string>>>(jsonDataText);
我有一个例外:
Cannot deserialize the current JSON object (e.g. {"name":"value"}) into type 'System.Collections.Generic.List`1[System.Collections.Generic.KeyValuePair`2[System.String,System.String]]' because the type requires a JSON array (e.g. [1,2,3]) to deserialize correctly.
也尝试过使用地图和字符串(多维)数组,但遇到了同样的异常......
【问题讨论】:
-
你尝试过创建一个包含太阳、行星、地球、星系的类吗?
-
我认为你的意思是
Dictionary<string, string>Newtonsoft 非常擅长将 JSON 对象转换为该模型。 -
@BugFinder 那些名字可以是任何东西,我无法命名。
-
@er-sho 我不知道,我不是该模型的创建者,只需要使用它。在发送到目标服务器之前,我需要对其进行反序列化,然后进行序列化(以相同的格式)。
-
@VladacusB 从您的帖子中不清楚这些名称正在改变
标签: c# json serialization json.net