【发布时间】:2017-06-11 17:45:06
【问题描述】:
我有以下 json 数据作为字符串
string data = "{\"STARTTIME\":\"12:00\",\"ENGINNEERSIGNATURE\":\"Engineer Signature .jpg\",\"OVERNIGHTS\":\"1\",\"SIGNOUT\":\"Yes\"}"
var dataOut = JsonConvert.DeserializeObject<Dictionary<string, string>>(data);
我需要将其转换为字典对象,但尝试时出现以下错误
Newtonsoft.Json.JsonSerializationException: Error converting value "{"STARTTIME":"12:00","ENGINNEERSIGNATURE":"Engineer Signature .jpg","OVERNIGHTS":"1","SIGNOUT":"Yes"}" to type 'System.Collections.Generic.Dictionary`2[System.String,System.String]'. Path '', line 1, position 119. ---> System.ArgumentException: Could not cast or convert from System.String to System.Collections.Generic.Dictionary`2[System.String,System.String].
at Newtonsoft.Json.Utilities.ConvertUtils.EnsureTypeAssignable (System.Object value, System.Type initialType, System.Type targetType) [0x00062] in <2781d1b198634655944cdefb18b3309b>:0
at Newtonsoft.Json.Utilities.ConvertUtils.ConvertOrCast (System.Object initialValue, System.Globalization.CultureInfo culture, System.Type targetType) [0x00031] in <2781d1b198634655944cdefb18b3309b>:0
at Newtonsoft.Json.Serialization.JsonSerializerInternalReader.EnsureType (Newtonsoft.Json.JsonReader reader, System.Object value, System.Globalization.CultureInfo culture, Newtonsoft.Json.Serialization.JsonContract contract, System.Type targetType) [0x0008d] in <2781d1b198634655944cdefb18b3309b>:0
--- End of inner exception stack trace ---
at Newtonsoft.Json.Serialization.JsonSerializerInternalReader.EnsureType (Newtonsoft.Json.JsonReader reader, System.Object value, System.Globalization.CultureInfo culture, Newtonsoft.Json.Serialization.JsonContract contract, System.Type targetType) [0x000bd] in <2781d1b198634655944cdefb18b3309b>:0
at Newtonsoft.Json.Serialization.JsonSerializerInternalReader.CreateValueInternal (Newtonsoft.Json.JsonReader reader, System.Type objectType, Newtonsoft.Json.Serialization.JsonContract contract, Newtonsoft.Json.Serialization.JsonProperty member, Newtonsoft.Json.Serialization.JsonContainerContract containerContract, Newtonsoft.Json.Serialization.JsonProperty containerMember, System.Object existingValue) [0x000d7] in <2781d1b198634655944cdefb18b3309b>:0
at Newtonsoft.Json.Serialization.JsonSerializerInternalReader.Deserialize (Newtonsoft.Json.JsonReader reader, System.Type objectType, System.Boolean checkAdditionalContent) [0x0007a] in <2781d1b198634655944cdefb18b3309b>:0
我哪里错了
【问题讨论】:
-
也添加你的错误
-
stackoverflow.com/help/how-to-ask "搜索和研究...并跟踪您发现的内容。即使您在网站的其他地方没有找到有用的答案,包括相关问题的链接" t help 可以帮助其他人了解您的问题与其他问题有何不同。” “包括任何错误消息”“如果可以创建一个可以链接到的问题的实时示例”
-
添加了错误信息 :)
-
我把你的代码放在 .net fiddle 中,它可以工作。 dotnetfiddle.net/yVBY89 这就是为什么“如果可以创建一个可以链接到的问题的实时示例”很重要。
-
我测试了你的代码并且它的工作正常。请指定您使用的 Newtonsoft.Json 版本。