转换JSON字符串c#时出现JsonConvert.DeserializeObject错误

时间:2017-01-26 09:28:21

标签: c# json json.net

我将以下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

我哪里出错

2 个答案:

答案 0 :(得分:0)

尝试使用Dictionary<string,object>,这将允许JSON的值为任何对象类型。 Dictionary<string,string>仅在JSON对象的所有值都是string类型时才有效。但是如果有任何其他值,比如数组或嵌套的JSON对象,它将失败。

答案 1 :(得分:0)

无法确定那里有什么问题,但是你能告诉我这个小程序在控制台的前两行打印,在你的示例代码在同一台机器上运行,我可以发现两个可能的问题Newtonsoft版本或者你本地培养

using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Reflection;
using System.Threading;
using Newtonsoft.Json;

namespace test
{
    class Program
    {
        static void Main(string[] args)
        {
            //Your culture
            Console.WriteLine("Your Culture:" + Thread.CurrentThread.CurrentCulture.Name);
            //your JsonConvert Assembly version
            Console.WriteLine("JsonConvert Assembly" + JsonConvertVersion());
            //I guess you have welsh culture, by your nickname
            //Thread.CurrentThread.CurrentCulture = new System.Globalization.CultureInfo("cy-GB");
            var stringified = "{\"STARTTIME\":\"12:00\",\"ENGINNEERSIGNATURE\":\"Engineer Signature .jpg\",\"OVERNIGHTS\":\"1\",\"SIGNOUT\":\"Yes\"}";
            Console.WriteLine(stringified);
            //Invariant culture witch shoudn't fail
            var settings = new JsonSerializerSettings() { Culture = System.Globalization.CultureInfo.InvariantCulture };
            var dataOut = JsonConvert.DeserializeObject<Dictionary<string, string>>(stringified, settings);
            //welsh culture, case I think that is what you are using, but for me is working 
            var settings2 = new JsonSerializerSettings() { Culture = new System.Globalization.CultureInfo("cy-GB", false) };//Welsh 
            var dataOut2 = JsonConvert.DeserializeObject<Dictionary<string, string>>(stringified, settings2);
            //object with invariant culture
            Console.WriteLine(dataOut);
            //object with welsh culture
            Console.WriteLine(dataOut2);
            Console.WriteLine(JsonConvert.SerializeObject(dataOut));
            Console.WriteLine(JsonConvert.SerializeObject(dataOut2));

        }
        public static string JsonConvertVersion()
        {
            Assembly asm = Assembly.GetAssembly(typeof(JsonConvert));
            FileVersionInfo fvi = FileVersionInfo.GetVersionInfo(asm.Location);
            return String.Format("{0}.{1}", fvi.FileMajorPart, fvi.FileMinorPart);

        }
    }
}