-4


I have this JSON Array in PHP which I receive from an Android request and I want to decode it so that I can get every value of each object in a loop and send them to a mysql database.

So in the example beneath, there's a class with two objects including properties FirstName and LastName. I would like to pars this json so that I can use them in a PDO function which inserts these rows in a mysql database.

[
{ "firstName":"John" , "lastName":"Doe" }, 
{ "firstName":"Anna" , "lastName":"Smith" }, 
{ "firstName":"Peter" , "lastName":"Jones" }
]

How do I accomplish this. After a lot of trial and error it still won't work. Thanks in advance!

Edit: json_decode worked after all. JSON syntax issue was the problem.

4

1 に答える 1

3

Use json_decode()

$json = '[
{ "firstName":"John" , "lastName":"Doe" }, 
{ "firstName":"Anna" , "lastName":"Smith" }, 
{ "firstName":"Peter" , "lastName":"Jones" }
]';
$json_decoded = json_decode($json);
var_dump($json_decoded);

See it in action

于 2013-03-01T15:21:25.767 に答える