我的Android应用程序将JSON字符串发布到基于PHP的slim框架。
这是JSON:
ItemList=[{"Address":"addresses 263838","CreatedDate":"2016-11-11 11:53:53","DeviceID":"1","ID":0,"Latitude":24.8715396,"Locality":"locality","Longitude":67.0898003,"Name":"Item ","OfflineId":20161111115352400,"Subchannel":"Subchannel","Sublocality":"Sublocality","ToUpdate":false,"imageLocalPath":"/storage/emulated/0/Pictures/1478847221674.jpg"},{"Address":"address","CreatedDate":"2016-11-11 17:46:41","DeviceID":"1","ID":0,"Latitude":24.87110129,"Locality":"Locality","Longitude":67.09033959,"Name":"Item 23","OfflineId":20161111174637550,"Subchannel":"Subchannel","Sublocality":"Sublocality","ToUpdate":false,"imageLocalPath":"/storage/emulated/0/Pictures/1478868360328.jpg"},{"Address":"Address 648483","CreatedDate":"2016-11-12 09:43:54","DeviceID":"1","ID":0,"Latitude":24.87952002,"Locality":"Locality","Longitude":67.09332882,"Name":"Item 25","OfflineId":20161112094353314,"Subchannel":"Subchannel","Sublocality":"Sublocality","ToUpdate":false,"imageLocalPath":"/storage/emulated/0/Pictures/1478925794392.jpg","imageName":"1478925794392.jpg"}]
Slim Framework(PHP)代码
$app->post('/itemlist', function ($request, $response) {
$input = $request->getParsedBody();
foreach($input as $item)
{
$sql = "INSERT INTO jsondump (jsondata) VALUES (:jsondump)";
$resQur = $this->db->prepare($sql);
$resQur->bindParam("jsondump", $item);
$resQur->execute();
}
return $this->response->withJson($input);
});
问题/挑战: JSON对象列表应该分解为JSON对象。目前我正在将JSON直接转储到数据库中以查看结果。它总是转储整个JSON字符串(包含整个列表)。
答案 0 :(得分:1)
如果在发布数据时content-type
设置为application/json
,那么$input
将自动解码。
考虑这个可调用的测试路径:
$app->post("/", function ($request, $response, $args) {
$input = $request->getParsedBody();
if ($input === null) {
echo "FAILED to decode JSON\n";
echo json_last_error_msg();
exit;
}
var_dump($input);
});
测试:
$ curl -i -H "Content-Type: application/json" \ http://localhost:8888 \ -d '[{"Address":"addresses 263838","CreatedDate":"2016-11-11 11:53:53","DeviceID":"1","ID":0,"Latitude":24.8715396,"Locality":"locality","Longitude":67.0898003,"Name":"Item ","OfflineId":20161111115352400,"Subchannel":"Subchannel","Sublocality":"Sublocality","ToUpdate":false,"imageLocalPath":"/storage/emulated/0/Pictures/1478847221674.jpg"},{"Address":"address","CreatedDate":"2016-11-11 17:46:41","DeviceID":"1","ID":0,"Latitude":24.87110129,"Locality":"Locality","Longitude":67.09033959,"Name":"Item 23","OfflineId":20161111174637550,"Subchannel":"Subchannel","Sublocality":"Sublocality","ToUpdate":false,"imageLocalPath":"/storage/emulated/0/Pictures/1478868360328.jpg"},{"Address":"Address 648483","CreatedDate":"2016-11-12 09:43:54","DeviceID":"1","ID":0,"Latitude":24.87952002,"Locality":"Locality","Longitude":67.09332882,"Name":"Item 25","OfflineId":20161112094353314,"Subchannel":"Subchannel","Sublocality":"Sublocality","ToUpdate":false,"imageLocalPath":"/storage/emulated/0/Pictures/1478925794392.jpg","imageName":"1478925794392.jpg"}]'
给出:
HTTP/1.1 200 OK Host: localhost:8888 Connection: close X-Powered-By: PHP/7.0.12 Content-Type: text/html; charset=UTF-8 array(3) { [0] => array(13) { 'Address' => string(16) "addresses 263838" 'CreatedDate' => string(19) "2016-11-11 11:53:53" 'DeviceID' => string(1) "1" 'ID' => int(0) 'Latitude' => double(24.8715396) 'Locality' => string(8) "locality" 'Longitude' => double(67.0898003) 'Name' => string(5) "Item " 'OfflineId' => int(20161111115352400) 'Subchannel' => string(10) "Subchannel" 'Sublocality' => string(11) "Sublocality" 'ToUpdate' => bool(false) 'imageLocalPath' => string(46) "/storage/emulated/0/Pictures/1478847221674.jpg" } [1] => array(13) { 'Address' => string(7) "address" 'CreatedDate' => string(19) "2016-11-11 17:46:41" 'DeviceID' => string(1) "1" 'ID' => int(0) 'Latitude' => double(24.87110129) 'Locality' => string(8) "Locality" 'Longitude' => double(67.09033959) 'Name' => string(7) "Item 23" 'OfflineId' => int(20161111174637550) 'Subchannel' => string(10) "Subchannel" 'Sublocality' => string(11) "Sublocality" 'ToUpdate' => bool(false) 'imageLocalPath' => string(46) "/storage/emulated/0/Pictures/1478868360328.jpg" } [2] => array(14) { 'Address' => string(14) "Address 648483" 'CreatedDate' => string(19) "2016-11-12 09:43:54" 'DeviceID' => string(1) "1" 'ID' => int(0) 'Latitude' => double(24.87952002) 'Locality' => string(8) "Locality" 'Longitude' => double(67.09332882) 'Name' => string(7) "Item 25" 'OfflineId' => int(20161112094353314) 'Subchannel' => string(10) "Subchannel" 'Sublocality' => string(11) "Sublocality" 'ToUpdate' => bool(false) 'imageLocalPath' => string(46) "/storage/emulated/0/Pictures/1478925794392.jpg" 'imageName' => string(17) "1478925794392.jpg" } }
即一个包含三个数组的数组。