如何使用 PHP Slim 框架解析特定类型的 JSON?
How to parse JSON of a specific type with PHP Slim framework?
我有 JSON 字符串被 Android 应用程序发布到我基于 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 框架(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);
});
Problem/Challenge:
JSON 对象列表应分为 JSON 个对象。目前我正在将 JSON 直接转储到数据库中以查看结果。它总是转储整个 JSON 字符串(包含整个列表)。
如果在发布数据时将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"
}
}
即一个包含三个数组的数组。
我有 JSON 字符串被 Android 应用程序发布到我基于 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 框架(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);
});
Problem/Challenge: JSON 对象列表应分为 JSON 个对象。目前我正在将 JSON 直接转储到数据库中以查看结果。它总是转储整个 JSON 字符串(包含整个列表)。
如果在发布数据时将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" } }
即一个包含三个数组的数组。