如何将json数组数据插入mysql?

时间:2015-05-04 02:57:57

标签: php android mysql arrays json

您好我正在尝试将json数组插入我的MySQL数据库。从Android客户端获取数组json数据。

{"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"}]}

如何将json数据解析成数据库?

$message_data = json_decode($data,true);

printf($message_data['message']);die;

1 个答案:

答案 0 :(得分:0)

您的数据:

{
    "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"
        }
    ]
}

当你json_decode($data)

它将是对象,就像这样

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
                )

        )

)

但是如果你这样做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
                )

        )

)

之后,您可以使用fore $message_data->message$message_data['message']

将其插入数据库