1

I'm using Newtonsoft.JSON to parse JSON files.

This is a JSON file I'm using:

[
  {
    "FilePath"    : "C:\Users\Administrator\Desktop\dummyFile.txt",
    "DisplayName" : "Dummy File"
  }
]

And I get the following error saying that it couldn't properly parse "\U" (from C:\Users.....) at

JsonTextReader reader = new JsonTextReader(new StringReader(File.ReadAllText(gamelist, Encoding.Unicode)));
while (reader.Read())
{
  //do stuff here
Hyblocker
  • 101
  • 4
  • 11

1 Answers1

1

@Hyblocker, replace the backslash as it is creating the problem. Below code works

//Read the json from gamelist file
var fileData = File.ReadAllText(gamelist);
//replace "\" with "\\"
fileData = fileData.Replace("\\", "\\\\");
//parse it
JsonTextReader reader = new JsonTextReader(new StringReader(fileData));
while (reader.Read())
{
       //code
}
  • While this might work for the simple JSON shown above, it is not a robust solution as the `DisplayName` property might contain slashes that don't need to be escaped. The only valid solution is to fix the source file. – DavidG Jun 22 '18 at 11:22
  • @DavidG, I agree with you. Even Newtonsoft has a limitation with special character. – Prateek Deshmukh Jun 22 '18 at 11:23