Unable to decode JSON string in php

I have the following JSON line, I'm trying to decode using php json_decode, but $ postarray is always NULL, can't understand why this is?

Running on Debian 5.0 Linux Client API version php => 5.0.51a Json version 1.2.1

 $json = '{\"json\":[{\"username\":\"1062576\",\"accountId\":\"45656565\"}]}';

 $postarray = json_decode($json);
 print_r($postarray);

thank

+3
source share
4 answers

The reason to avoid double quotes ( \") in a string is because the string contains double quotes.

Since you avoid double quotes, you should double (not one) the quote of your string, for example:

<?php
 $json = "{\"json\":[{\"username\":\"1062576\",\"accountId\":\"45656565\"}]}";

 $postarray = json_decode($json);
 print_r($postarray);
?>

Live example

If you need a single quote from your string, do not avoid double quotes or use stripslashes () , as suggested by Andrew.

PHP .

+9

:

<?php
$json = stripslashes('{\"json\":[{\"username\":\"1062576\",\"accountId\":\"45656565\"}]}');

$postarray = json_decode($json);
print_r($postarray);
+7

.

+2
source

The string will not be parsed because it is enclosed in single quotes, so backslashes are literal. If you delete them, use stripslashes or enclose the string in double quotes, you should have no problem.

+1
source

Source: https://habr.com/ru/post/1761962/


All Articles