2016-11-12 70 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