I have a json file in that title entity is there. I parse the title, it gives error because of '\' character in the title.
for(int i = 0; i < stringJSON.length() - 1; i++){
if(stringJSON.charAt(i) == character && !(stringJSON.charAt(i+1) == character)){
rawJSON = new StringBuilder(stringJSON).insert(i+1, '\\').toString();
i = i + 2;
System.out.println("found at " + i);
}
Input json file is as below-
{ "class":
{
"number": 2,
"student":
{
"title": "\\The first day \b of my life",
"age": 1
},
"student":
{
"title": "\\A corner in the \A home",
"age": 2
}
}
}
The solution above have lot of issues, like "\\" is a valid character, '\"' is a valid character.
Can somebody help me to find out any java package or API which helps to auto correct this kind of error?
In the second record of student, you can see a single slash. That makes json invalid.
My question is how to correct this json? I can either remove it or append one more slash to make it valid json string.
The string is combination of escape characters "\\", and control character '\'.
This answers what you are asking:
You can see there how the \ is a "control" character. If you want a literal backslash within your JSON, you'll need to escape it: "\\" is a valid JSON string, for example.
Source: JSON Valid Chars