I've big JSON something like this:
{
"EmployeeMaster": {
"ImageContent": null,
"ImageName": null,
"EMP_PhotoPath": "E:\BBM0000000001parison.png"
}
}
I'm trying to parse it, but its not working due to slash in EMP_PhotoPath
.
How can resolve this error ?
I've big JSON something like this:
{
"EmployeeMaster": {
"ImageContent": null,
"ImageName": null,
"EMP_PhotoPath": "E:\BBM0000000001parison.png"
}
}
I'm trying to parse it, but its not working due to slash in EMP_PhotoPath
.
How can resolve this error ?
Share Improve this question edited Mar 27, 2019 at 16:19 Joey Ciechanowicz 3,6633 gold badges29 silver badges49 bronze badges asked Feb 16, 2017 at 18:50 Mox ShahMox Shah 3,0153 gold badges29 silver badges44 bronze badges 9-
You need to escape the slash with a slash:
[...]th":"E:\\BBM00000[...]
– ssc-hrep3 Commented Feb 16, 2017 at 18:53 - 1 When you say "parse," do you mean JSON.parse? Because that works fine – KevBot Commented Feb 16, 2017 at 18:55
- 1 What are you using to generate the JSON? Any standard library that generates the JSON should automatically escape the slash. Are you generating it by hand? – Adam Jenkins Commented Feb 16, 2017 at 18:56
- 2 he wants to keep the \ – Ryad Boubaker Commented Feb 16, 2017 at 18:56
-
1
@MoxShah -
Newtonsoft.JSONConvert
has a bug in it if it's not escaping the slash.I doubt that, so I think your actual problem lies elsewhere. – Adam Jenkins Commented Feb 16, 2017 at 19:04
2 Answers
Reset to default 7
var jsonString = String.raw`{"EmployeeMaster":{"ImageContent":null,"ImageName":null,"EMP_PhotoPath":"E:\BBM0000000001parison.png"}}`;
jsonString = jsonString.replace("\\","\\\\");
var jsonObj = JSON.parse(jsonString);
alert(jsonObj.EmployeeMaster.EMP_PhotoPath);
You can Achieve this by doing something like this:
var jsonString = String.raw`{"EmployeeMaster":{"ImageContent":null,"ImageName":null,"EMP_PhotoPath":"E:\BBM0000000001parison.png"}}`;
jsonString = jsonString.replace("\\","\\\\");
var jsonObj = JSON.parse(jsonString);
String.raw is a method you can use to get the original string without interpretation,
It's used to get the raw string form of template strings (that is, the original, uninterpreted text).
So you can replace the backslash with double backslashes, then you can parse it to keep the original backslash.
You have to escape the slash with a second slash. Your valid json would look like that:
{
"EmployeeMaster": {
"ImageContent": null,
"ImageName": null,
"EMP_PhotoPath": "E:\\BBM0000000001parison.png"
}
}
ps: Paste it into JSONLint. to verifiy.