How to Remove the Slashes from Json Key While Converting Object into String

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



Leave a reply



Submit