Skip to content
Advertisement

Could not decode the JSON string using PHP

I could not convert the JSON string to array using PHP. Here is my code:

$edu=$_POST['edu'];
echo ($edu);

The above line is giving the below output.

'[{"uname":"univ1","year":"2017","description":"hello"},{"uname":"univ2","year":"2016","description":"hello des"}]' 

I need to convert this to array using below code.

$eduArr=json_decode($edu,true);
print_r($eduArr);

But here I am getting the output as (empty). I need to convert the above string to array.

Advertisement

Answer

You json string is not a valid json. It have extra ' on both side of the string.

From your echo result the ' is output on each side of the string. For a json string the ' shoudn’t be there.

You can check the live demo here have a good understanding.

$eduArr=json_decode(trim($edu, '''),true);
print_r($eduArr);
User contributions licensed under: CC BY-SA
1 People found this is helpful
Advertisement