M
M
MaximMRX2017-07-07 16:38:53
PHP
MaximMRX, 2017-07-07 16:38:53

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":""}

As you can see in the provider, the name of the bank is in quotes, and because of this, JSON collapses
. How to be?

Answer the question

In order to leave comments, you need to log in

4 answer(s)
A
Anatoly Medvedev, 2017-07-07
@balamyt92

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.

E
Eugene Volf, 2017-07-07
@Wolfnsex

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.
b) Crutch - something like this:
$json_string = str_replace ('""', '\"\"', $json_string)

A
Alexey Zakharov, 2017-07-07
@konratnox

so try

$json = htmlspecialchars_decode($json);
$json = str_replace(' "', ' ', $json);
$json = str_replace('""', '"', $json);

S
Shirshov Alexander, 2017-07-07
@Keanor

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;
}

Result:
Array
(
    [tx] => 5204759
    [status] => SUCCESS
    [date] => 04.07.2017
    [time] => 21
    [cash] => 7 210,00руб.
    [orig] => 7 000,00руб.
    [provider] => WebmoneyОАОБанк"ККБ
    [opnum] => R14702331
    [comment] => 
)

You can play to save the last quote in the name of the bank

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question