2017-04-15 67 views
-1

我有這個JSON數組,這將在MySQL表中插入給出圖像與多個記錄插入JSON數組到MySQL數據庫:如何像DB圖像

My Sql table image

[{"F_relation":"Son","F_name":"fhj","F_address_type":"Other","F_address":"55556","F_phone":"556"}, {"F_relation":"Husband","F_name":"fhj","F_address_type":"Other","F_address":"5566","F_phone":"889"}, {"F_relation":"Husband","F_name":"fhj","F_address_type":"Same","F_address":"ak, USA, 5566","F_phone":"89888789"}]

+0

你嘗試過這麼遠嗎? –

+0

那麼你面臨的問題是什麼? – Javed

+0

首先將json轉換爲數組,然後在數據庫中插入數組值 –

回答

1

使用json_decode到JSON字符串轉換爲rray。

$json='[{"F_relation":"Son","F_name":"fhj","F_address_type":"Other","F_address":"55556","F_phone":"556"},{"F_relation":"Husband","F_name":"fhj","F_address_type":"Other","F_address":"5566","F_phone":"889"},{"F_relation":"Husband","F_name":"fhj","F_address_type":"Same","F_address":"ak, USA, 5566","F_phone":"89888789"}]'; 
//store the json string in a variable 

$a=json_decode($json,true); //will convert json string to array 
$len=sizeof($a); 
for($i=0;$i<$len;$i++){ 
    $F_relation=$a[$i]['F_relation']; //retrieve values 
    $F_name=$a[$i]['F_name']; 
    $F_address_type=$a[$i]['F_address_type']; 
    $F_address=$a[$i]['F_address']; 
    $F_phone=$a[$i]['F_phone']; 

    //write your query to insert, perform other operation 
} 
0
var res = jQuery.parseJSON(response.responseText); 
var result = res.d; 

所以,你可以用它像一個數組:result[0].F_relation