2

how can convert URL encoded JSON sent from my android application into PHP array.Current format i am getting looks like below format.

[{\\\"product_id\\\":\\\"33\\\",\\\"amount\\\":\\\"1\\\"},{\\\"product_id\\\":\\\"34\\\",\\\"amount\\\":\\\"3\\\"},{\\\"product_id\\\":\\\"10\\\",\\\"amount\\\":\\\"1\\\"}]

How can i convert those data into below format


product_id    amount
   33         1 
   34         3 
   10         1 

Because i want to insert those data into MySQL database.Can anyone please help me regarding this problem.

Marcin Orlowski
  • 72,056
  • 11
  • 123
  • 141
Damith
  • 1,982
  • 3
  • 28
  • 42

3 Answers3

2

Use json_decode :

echo json_decode($jsonarr);
Mahmoud Gamal
  • 78,257
  • 17
  • 139
  • 164
  • 1
    @Damith, this means that the json passed to it cannot be decoded or if the encoded data is deeper than the recursion limit as explained in the php manual. – Mahmoud Gamal Sep 23 '12 at 10:09
2

this can be done by json_decode

$json = '{"foo": 12345}';

$obj = json_decode($json);
print $obj->{'foo'}; // 12345

Good read

--- common mistakes using json_decode()

NullPoiиteя
  • 56,591
  • 22
  • 125
  • 143
1

json_decode(jsonencoded string, TRUE) is correct way to encode json string.

But your json string encoded too deeper and recursive. your json encode string should as below

[{"product_id":9,"amount":500},{"product_id":9,"amount":500},{"product_id":9,"amount":500}]

Other wise, you will get NULL value

Marcin Orlowski
  • 72,056
  • 11
  • 123
  • 141
GBD
  • 15,847
  • 2
  • 46
  • 50