Merianos Nikos Merianos Nikos - 6 months ago 51
PHP Question

php json_decode error quoted object property name expected

I try to create a script that decoding a simple

JSON
string in
PHP
and I get the following error:

quoted object property name expected


The string I try to decode is the following:

{"values":[{"url":"http://www.google.com","matches":"http|www|google|com"},{"url":"http://www.yahoo.com","matches":"http|www|yahoo|com"}]}


and the code I use to decoded is the following:

json_decode( $json_string );


I also have try to validate my json string in some online json validators, and the string seems to be fine.

Can someone please help me ?

Do you think the problem exists because of the double quotes ?

Update #1

Definetelly was a debuging issue. I place my experience here just to help other devs may come accross the same issue in the feature:

The problem was that my variable that came with the json string was html encoded so instead of the following string :

{"values":[{"url":"http://www.google.com","matches":"http|www|google|com"},{"url":"http://www.yahoo.com","matches":"http|www|yahoo|com"}]}


my variable came with the following string inside:

{"values":[{"url":"http://www.google.com","matches":"http|www|google|com"},{"url":"http://www.yahoo.com","matches":"http|www|yahoo|com"}]}


The mistake by my side was that I used the
print_r
method instead of the
var_dump
. This had as a result to print out the
&quot
as
"
in my page.

Answer

The json string is valid, and it works. You can add true for the second parameter of json_decode, and you get back an array. Try the following:

$json_string = '{"values":[{"url":"http://www.google.com","matches":"http|www|google|com"},{"url":"http://www.yahoo.com","matches":"http|www|yahoo|com"}]}';

var_dump(json_decode($json_string, true));

It works for me. enter image description here