doufei8250 2015-05-04 02:57
浏览 377

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

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;
  • 写回答

1条回答 默认 最新

  • dpw50696 2015-05-04 06:06
    关注

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

    评论

报告相同问题?

悬赏问题

  • ¥15 c语言怎么用printf(“\b \b”)与getch()实现黑框里写入与删除?
  • ¥20 怎么用dlib库的算法识别小麦病虫害
  • ¥15 华为ensp模拟器中S5700交换机在配置过程中老是反复重启
  • ¥15 java写代码遇到问题,求帮助
  • ¥15 uniapp uview http 如何实现统一的请求异常信息提示?
  • ¥15 有了解d3和topogram.js库的吗?有偿请教
  • ¥100 任意维数的K均值聚类
  • ¥15 stamps做sbas-insar,时序沉降图怎么画
  • ¥15 买了个传感器,根据商家发的代码和步骤使用但是代码报错了不会改,有没有人可以看看
  • ¥15 关于#Java#的问题,如何解决?