4

I have the following json data as a string

 string  data =  "{\"STARTTIME\":\"12:00\",\"ENGINNEERSIGNATURE\":\"Engineer Signature .jpg\",\"OVERNIGHTS\":\"1\",\"SIGNOUT\":\"Yes\"}"
 var dataOut = JsonConvert.DeserializeObject<Dictionary<string, string>>(data);

I need to convert it to a dictionary object but I get the following error when trying

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

Where am I going wrong

5
  • add your error also Commented Jan 26, 2017 at 9:29
  • stackoverflow.com/help/how-to-ask "Search, and research...and keep track of what you find. Even if you don't find a useful answer elsewhere on the site, including links to related questions that haven't helped can help others in understanding how your question is different from the rest." "Include any error messages" "If it is possible to create a live example of the problem that you can link to" Commented Jan 26, 2017 at 9:30
  • error message added :) Commented Jan 26, 2017 at 9:34
  • 2
    I put your code in .net fiddle and it works. dotnetfiddle.net/yVBY89 Which is why "If it is possible to create a live example of the problem that you can link to" is important. Commented Jan 26, 2017 at 9:34
  • 1
    I test your code and its works correctly. Specify, please, what version of Newtonsoft.Json you use. Commented Jan 26, 2017 at 9:36

2 Answers 2

2

Try to use Dictionary<string,object> , this will allow value of JSON to be of any object type. Dictionary<string,string> will only work if all values of JSON object are of type string. But if there is any other value, like array or nested JSON object, it will fail.

Sign up to request clarification or add additional context in comments.

Comments

0

Cannot tell for sure what is wrong there, but can you tell me what this small program prints on first two lines in console, run on same machine with your sample code, I can spot two possible problems Newtonsoft version either with you local culture

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

        }
    }
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.