PHP json_decode not working - displays NULL output

I am trying to use a JSON decoder to get some information, but it doesn’t work, it just shows the data as null when I use var_dump

Here is the JSON format data passed in URL

orderSummary={"orderInfo":[{"itemNumber":"1","quantity":"3","price":"5.99","productName":"Item_B"}]} 

When I just repeat the jagged line, I get the following

 echo $_GET['orderSummary']; //displays the following {\"orderInfo\":[{\"itemNumber\":\"1\",\"quantity\":\"3\",\"price\":\"5.99\",\"productName\":\"Item_B\"}]} 

However, when I try to decode it, the result is null

 $order = $_GET['orderSummary']; $data = json_decode($order,true); echo "<PRE>"; var_dump($data); die(); //displays the following <PRE>NULL 

Is it formatted correctly?

+4
source share
1 answer

First run the input string stripslashes() .

 $input = '{\"orderInfo\":[{\"itemNumber\":\"1\",\"quantity\":\"3\",\"price\":\"5.99\",\"productName\":\"Item_B\"}]}'; print_r(json_decode(stripslashes($input))); 

Output

 stdClass Object ( [orderInfo] => Array ( [0] => stdClass Object ( [itemNumber] => 1 [quantity] => 3 [price] => 5.99 [productName] => Item_B ) ) ) 

Demo

Alternatively

Disable magic_quotes_gpc . Given that it is deprecated (and removed in 5.4), this is the best option .

+12
source

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


All Articles