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



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

我哪里出错了

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

无法确定那里出了什么问题,但是您能否告诉我这个小程序在控制台的前两行打印了什么,与您的示例代码在同一台机器上运行,我可以发现两个可能的问题 牛顿软件版本要么与你当地文化

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);
        }
    }
}

相关内容

  • 没有找到相关文章

最新更新