Remove back slash from Json String in android
The command
result.replaceAll("\\","");
is correct and if you try to display the server response with an online json formatter (https://jsonformatter.curiousconcept.com/) you can clearly see that the string is not correctly formatted. The double quotes after the "response" and one at the end are not required.
Remove Backslashes from Json Data in JavaScript
Your string is invalid, but assuming it was valid, you'd have to do:
var finalData = str.replace(/\\/g, "");
When you want to replace all the occurences with .replace
, the first parameter must be a regex, if you supply a string, only the first occurrence will be replaced, that's why your replace wouldn't work.
Cheers
how to remove the slash in json object in android?
This should do the job
String newJson = oldJson.replaceAll("\\\\", "");
How do I remove backslashes before quotes from a JSONObject string?
You are creating a list of string, which is not what you want.
You should instead create a list of objects (Maps)
Map<String, Object> m1 = new LinkedHashMap<>();
m1.put("name", "Bob");
m1.put("age", 40);
m1.put("favorite_pizza", "Cheese");
LinkedHashMap<String, Object> m2 = new LinkedHashMap<>();
m2.put("name", "Jill");
m2.put("age", 22);
m2.put("favorite_candy", "Swedish Fish");
List<LinkedHashMap<String, Object>> records = Arrays.asList(m1,m2);
JSONObject body = new JSONObject();
// Add the "records" key
body.put("records", records);
This is a quite common mistake (it seems), to try to serialize strings formatted like json objects expecting is the same thing as passing a the object itself.
UPDATE:
Or if you have a json serialized object list then ...
List<String> recordSource =
Arrays.asList(
"{\"name\":\"Bob\",\"age\":40,\"favorite_pizza\":\"Cheese\"}",
"{\"name\":\"Jill\",\"age\":22,\"favorite_candy\":\"Swedish Fish\"}");
List<JSONObject> records =
recordSource.stream().map(JSONObject::new).collect(Collectors.toList());
JSONObject body = new JSONObject();
// Add the "records" key
body.put("records", records);
System.out.println(body.toString());
removing the backslashes in json string using the javascript
Actually the problem is not with /
slashs. The JSON is INVALID
.
remove these "
from backend server
{"_body":"{\"timestamp\":\"2016-11-18T04:46:18.972+0000\",\"status\":500,\"error\":\"Internal
Server
Error\",\"exception\":\"java.lang.ArrayIndexOutOfBoundsException\",\"message\":\"1\",\"path\":\"/login\"}","status":500,"ok":false,"statusText":"Internal
Server Error"}
double quote
before "{"timestamp and one after login"}"
these two highlighted and your code will work.
var data = '{"_body":{\"timestamp\":\"2016-11-18T04:46:18.972+0000\",\"status\":500,\"error\":\"Internal Server Error\",\"exception\":\"java.lang.ArrayIndexOutOfBoundsException\",\"message\":\"1\",\"path\":\"/login\"},"status":500,"ok":false,"statusText":"Internal Server Error"}';
var json_data = JSON.parse(data);
console.log(json_data);
Related Topics
Java Simpledateformatter With 10 Decimals After the Seconds, Cannot Convert to Date
Multiple Scanner Inputs (Java)
How to Disable Spring Security for Particular Url
How to Solve Maven 2.6 Resource Plugin Dependency
How to Properly Re-Run Spring Boot Application from Eclipse
Where Does Gradle Save Dependencies' Jars
Android - Running a Background Task Every 15 Minutes, Even When Application Is Not Running
How to Add Multiple Components to a Jframe
Geting Http Status 400 - Required Multipartfile Parameter 'File' Is Not Present in Spring
How to Send Parallel Get Requests and Wait for Result Responses
How to Process Lines of a File in Parallel
How to Combine Date and Time into a Single Object
Splitting String With Parentheses
How to Make a Java Program Quit When "Q" Is Inputted, Issue Is the Default Input Variable Is Double
How to Resolve Java.Lang.Noclassdeffounderror: Javax/Xml/Bind/Jaxbexception
Eclipse No Tests Found Using Junit 5 Caused by Noclassdeffounderror for Launcherfactory
Mailconnectexception: Couldn't Connect to Host, Port: Smtp.Sendgrid.Net