Can't decode JSON string in php - php

I have the following JSON string, i try to decode with php json_decode but $postarray
is always NULL, can't work out why this is?
Running on Debian 5.0 Linux
php Client API version => 5.0.51a
Json version 1.2.1
$json = '{\"json\":[{\"username\":\"1062576\",\"accountId\":\"45656565\"}]}';
$postarray = json_decode($json);
print_r($postarray);
Thanks

The reason to escape double quotes (\") in a string, is if the string is double quoted.
Since you are escaping the double quotes, you should double (not single) quote your string, like this:
<?php
$json = "{\"json\":[{\"username\":\"1062576\",\"accountId\":\"45656565\"}]}";
$postarray = json_decode($json);
print_r($postarray);
?>
Live Example
If you do want to single quote your string, then don't escape the double quotes, or use stripslashes() like Andrei suggested.
You can read about the four ways to specify a string in PHP, and the differences among them, here.

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

You should enclose it in double quotes.

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

Related

Regex how to escape double quotes if needed

I need to convert all big integers to strings in my response json. Currently my regex look like this
preg_replace(
'/:\s*(-?\d{16,})/',
': "$1"',
$content
);
But problem with such regex is that if my response contains another json string then bigints inside it will be wrapped in string too, but without escaping. Is there any way to escape appended quotes in such case? Or maybe fix incorrect json with another regex?
Example
{"example_bigint": 3330922503411457761}
will be converted to
{"example_bigint": "3330922503411457761"}
but
{"example_json" : "{\"example_bigint\": 3330922503411457761}"}
will be converted to
{"example_json" : "{\"example_bigint\": "3330922503411457761"}"}
when expected output is
{"example_json" : "{\"example_bigint\": \"3330922503411457761\"}"}
There is a flag in the json_decode function:
$myJson = json_decode($jsonString, flags: JSON_BIGINT_AS_STRING);

php escaping double quotes

Hi I'm having some difficulty with escaping double quoutes from a string.
Here's my situation:
I get the the result set from the database then I apply utf8_encode to it because there's latin/accented characters and it return the string as it should be exept the double quotes in the begin and end of the string.
If in the DB I have: "Olá João" it returns: Olá João. The double quotes are ignored
$rs = mysql_fetch_array($query)
$text = utf8_encode($rs['l_reference']);
echo $text;
I tried using addslashes but without success.
I think its because "Olá João" is a multibyte string and you must use a different workaround for this. Try this one mb_addslashes

PHP str_replace doesn't work as expected

I'm trying to use str_replace to correct a filepath as shown below:
$a="F:\xampp\htdocs\yii\get_smart\Music\mix\English\1636464449";
$a=str_replace('\\','/', $a);
echo $a;
returns:
F:
mpp/htdocs/yii/get_smart/Music/mix/Englishs6464449
Can someone please tell me what I'm doing wrong?
My PHP version is 5.3.8
Use single quote for define $a
$a='F:\xampp\htdocs\yii\get_smart\Music\mix\English\1636464449';
the problem is not str_replace but the string defined within double quotes. The backslashes escape the x and other character after it.
This is happening because your string is in double quotes, so the \x is being parsed as a character.
Actually, it's trying to read \xam as a character. Docs: http://php.net/manual/en/regexp.reference.escape.php
Put your string in single quotes (or escape the slash before the x).
Your problem is that the first string has some escaped sequences. For example \xam has a meaning in php. It looks like \16 might also mean something. You should echo $a before you do the str_replace and see what you get.

basics of php strings

//i am really very confused about what is used to represent a string in php i.e. if we use double inverted commas it too represents string and same if use single inverted commas
so "avinash" or 'avinash'........which is a string?
//and plz can u tell me about a good book to read php5 from
Double quotes (") and single quotes (') both represent strings. However, PHP doesn't treat them the same way.
In double quoted (") strings, escape sequences and variable names are expanded. In single quotes (') strings, they are not. The string is not expanded.
So, given the following:
$name = "Foo";
The following code...
$doubleQuotedString = "Hello $name.\nIt is nice to see you.";
echo $doubleQuotedString;
... will output this:
Hello Foo.
It is nice to see you.
However, the following...
$singleQuotedString = 'Hello $name.\nIt is nice to see you.';
echo $singleQuotedString;
... will output this:
Hello $name.\nIt is nice to see you.
For more information, you can read the following documentation page on strings.
PHP Documentation: Strings

issues with quotes in json string :php

I have tried lots of things and could not get through yet. I have a json string with some single quotes, double quotes and some other characters which are causing an error when I am trying to iterate the json by converting in into an array.
The quotes are escapes using addslashes and are going correctly in the database. But when I am converting the string to array using json_decode and stripslashes it says invalid argument passed to for loop.
Please suggest.
Thanks in advance.
Do this on the quotes:
$str = htmlentities($str, ENT_QUOTES);
And they will get-off your way.
As I remember, you have to first escape the quotes in php, and then escape the quotes and slashes when you print the json_encode. So, for "\"text\"" the output has to be \"\\"text\\"\".

Categories