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
remove backslash from json string
You can try the following way to achieve what you need,
$jsonString = '\"[\n {\n \"id\": \"1\",\n \"mid\": \"1\",\n \"num\": \"1\",\n \"type\": \"wgp\",\n \"time_changed\": \"time\",\n \"username\": \"aaa\"\n }\n ]\"';
$jsonString = str_replace('\\n', '', $jsonString); // First remove the new line characters in the json string
$jsonString = str_replace('\\', '', $jsonString); // Replace backslash with empty string
echo preg_replace('!\s+!', ' ', $jsonString); // Replace multiple spaces with one
So the output is,"[ { "id": "1", "mid": "1", "num": "1", "type": "wgp", "time_changed":
"time", "username": "aaa" } ]"
Remove all occurrences of \ from string
Try this:
String jsonFormattedString = jsonStr.replaceAll("\\\\", "");
Because the backslash is the escaping character in a regular expression (replaceAll()
receives one as parameter), it has to be escaped, too. Remove Backslash from JSON string?
I am not very sure your is a function or not. Anyways, the response you receive is having the literal character ' and you need to replace it with ".
Here is the working code:
import urllib2,json
headers = {'User-Agent': 'Mozilla/5.0'}
req = urllib2.Request("http://spatialreference.org/ref/esri/nad-1983-stateplane-california-vi-fips-0406-feet/json/", None, headers)
response = urllib2.urlopen(req)
response_read = response.read()
epsg_json = json.loads(response_read.replace("\'", '"'))
epsg_code = epsg_json['properties']['code']
print(epsg_code)
Hope this helps. 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);
Remove back slashes from JSON response
You just need to deserialize the body.
let err = { "_body": "{\"error\":\"264\",\"message\":\"Please enter valid usename/password\",\"object\":null}", "status": 400, "ok": false }
var body = JSON.parse(err._body);console.log(body.message);
Related Topics
Docker-Compose Restart Connection Pool Full
Error While Starting Puma Server with Workers
Define_Method with Predefined Keyword Arguments
How to Troubleshoot Memory Bloat at Boot for Rails App
Is It Still Advisable to Test Routes in Rails 4 with Minitest
How to Send a Keep-Alive Packet Through Websocket in Ruby on Rails
How Can Multiple Trailing Slashes Can Be Removed from a Url in Ruby
How to Make Devise Registrationscontroller to Show Sign_Up Page Only If User Is Already Signed In
How to Wait for System Command to End
Ruby: How to Escape Url with Square Brackets [ and ]
Running "Bundle Install" Fails and Asks Me to Run "Bundle Install"
How to Catch an "Undefined Method '[]' for Nil:Nilclass" Error
Rails on Netbeans: Uncaught Exception: No Such File to Load - Script/Server or Script/Console