2015-05-04 64 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']