-
Anonymous
Удалить обратную косую черту из строки JSON
Сообщение
Anonymous »
Я получаю следующую строку json из ответа сервера:
Код: Выделить всё
"\"[\n {\n \"id\": \"1\",\n \"mid\": \"1\",\n \"num\": \"1\",\n \"type\": \"wgp\",\n \"time_changed\": \"time\",\n \"username\": \"aaa\"\n }\n ]\""
Мне нужно переформатировать его так:
Код: Выделить всё
"{ { "id": "1", "mid": "1", "num": "1", "type": "wgp", "time_changed": "time", "username": "aaa" } }
вот моя попытка
Код: Выделить всё
$str = substr($str, 2);
$str = "{".$str;
$str = substr($str,0, strlen($str) - 3);
$str = $str . " }";
$str = trim(preg_replace('/\s\s+/', ' ', $str));
это дает мне
Код: Выделить всё
"{ { \"id\": \"1\", \"mid\": \"1\", \"num\": \"1\", \"plating_type\": \"wgp\", \"time_changed\": \"time\", \"username\": \"09122099111\" } }"
Мне не удалось удалить обратную косую черту, я пытался
Код: Выделить всё
$str= preg_replace('/\\\"/', '', $str);
$str = str_replace('\\', '', $str);
stripslashes ()
но ни один из них не сработал
Подробнее здесь:
https://stackoverflow.com/questions/580 ... son-string
1722081257
Anonymous
Я получаю следующую строку json из ответа сервера:
[code]"\"[\n {\n \"id\": \"1\",\n \"mid\": \"1\",\n \"num\": \"1\",\n \"type\": \"wgp\",\n \"time_changed\": \"time\",\n \"username\": \"aaa\"\n }\n ]\""
[/code]
Мне нужно переформатировать его так:
[code]"{ { "id": "1", "mid": "1", "num": "1", "type": "wgp", "time_changed": "time", "username": "aaa" } }
[/code]
вот моя попытка
[code] $str = substr($str, 2);
$str = "{".$str;
$str = substr($str,0, strlen($str) - 3);
$str = $str . " }";
$str = trim(preg_replace('/\s\s+/', ' ', $str));
[/code]
это дает мне
[code]"{ { \"id\": \"1\", \"mid\": \"1\", \"num\": \"1\", \"plating_type\": \"wgp\", \"time_changed\": \"time\", \"username\": \"09122099111\" } }"
[/code]
Мне не удалось удалить обратную косую черту, я пытался
[code]$str= preg_replace('/\\\"/', '', $str);
$str = str_replace('\\', '', $str);
stripslashes ()
[/code]
но ни один из них не сработал
Подробнее здесь: [url]https://stackoverflow.com/questions/58078970/remove-backslash-from-json-string[/url]