Hi I'm trying to insert the json array into my MySQL database.With array json data from android client.
{"message":[ {"body":"Fdsa","_id":"114","status":"-1","address":"null","read":"1","type":"3","date":"1429781969573","thread_id":"2"},{"body":"wtf2","_id":"113","status":"0","address":"0123456789","read":"1","type":"1","date":"1429590050090","thread_id":"1"}, {"body":"wtf2","_id":"112","status":"0","address":"0123456789","read":"1","type":"1","date":"1429590050090","thread_id":"1"}]}
how to parse json data into database ?
$message_data = json_decode($data,true);
printf($message_data['message']);die;
your data:
{
"message": [
{
"body": "Fdsa",
"_id": "114",
"status": "-1",
"address": "null",
"read": "1",
"type": "3",
"date": "1429781969573",
"thread_id": "2"
},
{
"body": "wtf2",
"_id": "113",
"status": "0",
"address": "0123456789",
"read": "1",
"type": "1",
"date": "1429590050090",
"thread_id": "1"
},
{
"body": "wtf2",
"_id": "112",
"status": "0",
"address": "0123456789",
"read": "1",
"type": "1",
"date": "1429590050090",
"thread_id": "1"
}
]
}
when you json_decode($data)
it will be an object like this
stdClass Object
(
[message] => Array
(
[0] => stdClass Object
(
[body] => Fdsa
[_id] => 114
[status] => -1
[address] => null
[read] => 1
[type] => 3
[date] => 1429781969573
[thread_id] => 2
)
[1] => stdClass Object
(
[body] => wtf2
[_id] => 113
[status] => 0
[address] => 0123456789
[read] => 1
[type] => 1
[date] => 1429590050090
[thread_id] => 1
)
[2] => stdClass Object
(
[body] => wtf2
[_id] => 112
[status] => 0
[address] => 0123456789
[read] => 1
[type] => 1
[date] => 1429590050090
[thread_id] => 1
)
)
)
but if you do this json_decode($data, true)
Array
(
[message] => Array
(
[0] => Array
(
[body] => Fdsa
[_id] => 114
[status] => -1
[address] => null
[read] => 1
[type] => 3
[date] => 1429781969573
[thread_id] => 2
)
[1] => Array
(
[body] => wtf2
[_id] => 113
[status] => 0
[address] => 0123456789
[read] => 1
[type] => 1
[date] => 1429590050090
[thread_id] => 1
)
[2] => Array
(
[body] => wtf2
[_id] => 112
[status] => 0
[address] => 0123456789
[read] => 1
[type] => 1
[date] => 1429590050090
[thread_id] => 1
)
)
)
after that you can insert it to db with foreach your $message_data->message
or $message_data['message']