2016-11-12 43 views
0

我有JSON字符串由Android应用发布到我的基于PHP的苗条框架。如何使用PHP Slim框架解析特定类型的JSON?

这里是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"}] 

苗条框架(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

使用'json_decode'然后遍历它。 –

+0

然后如何循环。我在使用foreach时遇到困难。没有得到预期的结果。 – Abubaker

+0

您应该使用foreach循环访问它,然后使用特定的键来获取该键的数据。请提供您已完成的代码以及您遇到问题的位置。 –

回答

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" 
    } 
} 

即用在其内三个阵列的阵列。

+0

感谢@Rob Allen的帮助。是的,我得到它的工作。将来我会记住你的建议。感谢您的好解释。 – Abubaker