嗨我試圖插入到我的MySQL數據庫json數組。從android客戶端數組json數據。如何使用php插入json數組到mysql數據庫
[{"name":"peter","phone":"dsf","city":"sdfsdf","email":"dsf"},{"name":"111","phone":"222","city":"hn","email":"[email protected]"}]
嗨我試圖插入到我的MySQL數據庫json數組。從android客戶端數組json數據。如何使用php插入json數組到mysql數據庫
[{"name":"peter","phone":"dsf","city":"sdfsdf","email":"dsf"},{"name":"111","phone":"222","city":"hn","email":"[email protected]"}]
你可以使用 - serialize()
$json = '[{"name":"peter","phone":"dsf","city":"sdfsdf","email":"dsf"},{"name":"111","phone":"222","city":"hn","email":"[email protected]"}]';
$newJson = serialize(json_decode($json));
$newJson
準備被插入。和打水後 -
$data = unserialize($fetchedData);
然後json_encode($data);
使用$jsonArray = json_decode($jsonStr);
。 然後迭代數組,以便將數據保存到mysql數據庫中。
如果你想存儲陣列作爲一個字符串,你可以使用JSON.stringify():
$string = [{"name":"peter","phone":"dsf","city":"sdfsdf","email":"dsf"},{"name":"111","phone":"222","city":"hn","email":"[email protected]"}];
$json = JSON.stringify($string);
變量$ JSON是那麼簡單的字符串,它可以很容易地插入到MySQL。
然後,您可以使用:
var obj = JSON.parse($json);
將字符串轉換回一個數組。
雖然這種方法通常不被推薦用於性能方面,所以你可能想要分解數組並單獨存儲每個字段。
試試這個:
$json = serialize(json_array);