2015-05-04 124 views
1

嗨我试图插入到我的MySQL数据库json数组。从Android客户端数组json数据。如何将json数组数据插入到mysql中?

{"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":"","read":"1","type":"1","date":"1429590050090","thread_id":"1"}, {"body":"wtf2","_id":"112","status":"0","address":"","read":"1","type":"1","date":"1429590050090","thread_id":"1"}]} 

如何解析json数据到数据库中?

$message_data = json_decode($data,true); 

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

哪里是MySQL的代码? – Raptor

+0

一旦你用这个'true'标志解码了JSON字符串,它就会成为你的普通数组。就像平常一样对待它,并使用PDO或MySQLi API来插入 – Ghost

+0

,但不解析字符串“message” –

回答

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": "", 
      "read": "1", 
      "type": "1", 
      "date": "1429590050090", 
      "thread_id": "1" 
     }, 
     { 
      "body": "wtf2", 
      "_id": "112", 
      "status": "0", 
      "address": "", 
      "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] =>
        [read] => 1 
        [type] => 1 
        [date] => 1429590050090 
        [thread_id] => 1 
       ) 

      [2] => stdClass Object 
       (
        [body] => wtf2 
        [_id] => 112 
        [status] => 0 
        [address] =>
        [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] =>
        [read] => 1 
        [type] => 1 
        [date] => 1429590050090 
        [thread_id] => 1 
       ) 

      [2] => Array 
       (
        [body] => wtf2 
        [_id] => 112 
        [status] => 0 
        [address] =>
        [read] => 1 
        [type] => 1 
        [date] => 1429590050090 
        [thread_id] => 1 
       ) 

     ) 

) 

你可以插入它用foreach分贝的$message_data->message$message_data['message']