简体   繁体   中英

Escape an already encoded json string

at the moment i have to work with some json strings where the quotes are not correctly escaped. The strings looks like this

{ "foo" : "hello my name is "michael"" }

Is there any realistic chance in JS/PHP to escape the quotes in the value without doing it manually so i can parse the string?

You haven't provided us much to work with, but it looks like you're generating the json something like this way:

$userInput = $_GET['userInput'];
$json = '{ "foo" : "' . $userInput . '" }';

This is pretty bad. Here's the appropriate way to generate the json safely:

$outputData = array(
  "foo" => $_GET['userInput']
);
$json = json_encode($outputData);

See the reference here: http://php.net/manual/en/function.json-encode.php

As to your original question, Is there any realistic chance in JS/PHP to escape the quotes? No. Suppose the "actual value" of the string is a series of paragraphs containing quote marks, you know, like a continuation of a quotation, where each paragraph starts with a " . No, that is not able to be fixed.

You need to fix the source of your json. If you are getting that json string from some third-party service, you need to tell them that the strings they are sending you is not valid json .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM