Stack Overflow is a community of 4.7 million programmers, just like you, helping each other.

Join them; it only takes a minute:

Sign up
Join the Stack Overflow community to:
  1. Ask programming questions
  2. Answer and help your peers
  3. Get recognized for your expertise

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;
share|improve this question
    
Where is the MySQL codes ? – Raptor May 4 '15 at 2:59
    
once you've decoded the JSON string with that true flag, it'll be just your ordinary array. just treat it as you normally would and use PDO or MySQLi API to make your insertions – Ghost May 4 '15 at 2:59
    
but not parse to string "message" – long trần May 4 '15 at 3:06
    
$message_data = json_decode($data,true); is an array, so you can just then do $message_data['message']; to get everything else – Augwa May 4 '15 at 3:09
    
Keep in mind that you are passing null as text - it will not work as expected (address). – Arkadiusz 'flies' Rzadkowolski May 4 '15 at 6:11

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']

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.