【问题标题】:JsonConvert.DeserializeObject error when converting JSON string c#JsonConvert.DeserializeObject 转换 JSON 字符串时出错 c#
【发布时间】: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 版本。

标签: c# json json.net


【解决方案1】:

尝试使用 Dictionary&lt;string,object&gt; ,这将允许 JSON 的值是任何对象类型。 Dictionary&lt;string,string&gt; 仅在 JSON 对象的所有值都是字符串类型时才有效。但是如果有其他值,比如数组或者嵌套的 JSON 对象,就会失败。

【讨论】:

    【解决方案2】:

    无法确定那里出了什么问题,但你能告诉我这个小程序在控制台的前两行打印什么,用你的示例代码在同一台机器上运行,我可以在你本地发现两个可能的问题 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);
    
            }
        }
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2012-07-29
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-03-19
      • 2013-12-15
      相关资源
      最近更新 更多