Answer the question
In order to leave comments, you need to log in
In json response, quotes need to be decoded in PHP?
Good day! I receive a JSON response from a remote server
But sometimes some responses contain quotes and the json_decode function breaks.
Here is the JSON at which the response breaks
{"tx":"5204759","status":"SUCCESS","date":"04.07.2017","time":"21:53:27","cash":"7 210,00руб.","orig":"7 000,00руб.","provider":"WebmoneyОАОБанк"ККБ"","opnum":"R14702331","comment":""}
Answer the question
In order to leave comments, you need to log in
This is not a valid JSON, try replacing "" with \"" before json_decode
UPD: you need to process regular expressions, a simple replacement will not work.
As you can see in the provider, the name of the bank is in quotes, and because of this, JSON collapses. How to be?a) Optimal - Ask the person who wrote the remote service to fix such an obvious JSON encoding bug and hint to him that if he himself cannot do it normally, let him use ready-made libraries.
$json_string = str_replace ('""', '\"\"', $json_string)
so try
$json = htmlspecialchars_decode($json);
$json = str_replace(' "', ' ', $json);
$json = str_replace('""', '"', $json);
Yab got sick ;)
function no_json_decode($encoded) {
$decoded = [];
foreach (explode('","', trim($encoded, '{}')) as $element) {
list($key, $value) = explode(':', $element);
$decoded[trim($key, '"')] = trim($value, '"');
}
return $decoded;
}
Array
(
[tx] => 5204759
[status] => SUCCESS
[date] => 04.07.2017
[time] => 21
[cash] => 7 210,00руб.
[orig] => 7 000,00руб.
[provider] => WebmoneyОАОБанк"ККБ
[opnum] => R14702331
[comment] =>
)
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question