0

Error

An unhandled exception of type 'Newtonsoft.Json.JsonReaderException' occurred in System.Private.CoreLib.dll

Unexpected character encountered while parsing value: S. Path '', line 0, position 0.

The json

{
   "Australia":"36",
   "Austria":"12",
   "Belgium":"2",
   "Bulgaria":"15",
   "Canada":"37",
   "China,mainland":"39",
   "Croatia":"21",
   "Cyprus":"16",
   "Czech Republic":"29",
   "Denmark":"10",
   "Estonia":"17",
   "Finland":"9",
   "France":"4",
   "Germany":"3",
   "Greece":"18",
   "Hong Kong SAR":"44",
   "Hungary":"19",
   "Ireland":"20",
   "Italy":"7",
   "Japan":"40",
   "Latvia":"22",
   "Liechtenstein":"43",
   "Lithuania":"24",
   "Luxembourg":"23",
   "Malta":"25",
   "Monaco":"42",
   "New Zealand":"38",
   "Norway":"32",
   "Poland":"8",
   "Portugal":"14",
   "Romania":"26",
   "Singapore":"47",
   "Slovakia":"28",
   "Slovenia":"27",
   "Spain":"6",
   "Sweden":"5",
   "Switzerland":"41",
   "The Netherlands":"1",
   "United Arab Emirates":"35",
   "United Kingdom":"13",
   "United States":"30"
}

My string is saved on a Properties.Resources inside my project. Running this makes the program crash:

var test= JsonConvert.DeserializeObject<Dictionary<string, string>>(UTF8.GetString(Properties.Resources.country));

code link: https://dotnetfiddle.net/o1Z3uv

Peter Csala
  • 17,736
  • 16
  • 35
  • 75
  • Try saving `UTF8.GetString(Properties.Resources.country` to a variable and check to see if that looks like the expected JSON. – juharr Jun 16 '21 at 19:00
  • I'm unable to reproduce your issue (without using UTF8.GetString()): https://dotnetfiddle.net/TFdy1Q. Can you provide a [mre]? – gunr2171 Jun 16 '21 at 19:03
  • Hi @juharr, thanks for your reply. I just did that and it exactly looks the same, also both jsons work on https://jsonformatter.curiousconcept.com/# – Emanuel Rodriguez Jun 16 '21 at 19:06
  • @gunr2171 I added a dotnetfiddler link, somehow is working there, but locally on my net core 3.1 project it crashes. – Emanuel Rodriguez Jun 16 '21 at 19:10
  • 1
    I would verify that the string is being loaded like you expect from Resources, and that it is still valid JSON (quotes escaped properly). – Jesse McConahie Jun 16 '21 at 19:36
  • @JesseMcConahie I run the code like it is on the dotnetfiddle example and it crashes still, I don't know what to do tbh. – Emanuel Rodriguez Jun 16 '21 at 21:06
  • @EmanuelRodriguez Which dotnetfiddle example does crash? The one that is provided in the question works fine. Change `Console.WriteLine(dict);` to `dict.Dump();` to see the parsed values. – Peter Csala Jun 17 '21 at 12:10
  • @PeterCsala theres no example that crashes, the issue is that online example doesn't crash, but on my computer does, and its the exact same. – Emanuel Rodriguez Jun 17 '21 at 12:27
  • @EmanuelRodriguez Can you please amend your question to include the result of `UTF8.GetString(Properties.Resources.country)`? Please also try to fetch the value like this `Properties.Resources.ResourceManager.GetString("country");` – Peter Csala Jun 17 '21 at 13:05
  • @PeterCsala I just confirmed that it was an issue with the type of the framework, still no solution on net core 3.1. I tried the code in the net framework and it works straight away. but on Net Core it crashes. I don't understand. – Emanuel Rodriguez Jun 17 '21 at 20:20
  • If you're using NuGet Packages, try uninstalling and reinstalling the Newtonsoft package. If the NuGet package was installed targeting the wrong framework, sometimes that can cause issues. This can happen if you converted your app from Framework to Core. – Jesse McConahie Jun 18 '21 at 14:44

0 Answers0