2017-03-05 63 views
0

我该如何解析这种类型的JSON?所有我能找到的信息是关于包括一个表名的数据,但我是一个没有表名,只是这样的colums:从slim微型框架API解析android上的JSON

{ 
    "fk_Company": "1", 
    "id_Article": "42614", 
    "Reference": "M520345", 
    "Designation": "FEUX AVD BOXER", 
    "fk_ArtFam": null, 
    "fk_ArtTyp": null, 
    "fk_ArtCatg": null, 
    "fk_Marque": null, 
    "TxMarge": " 25 ", 
    "PVTTC": " 14.479 ", 
    "Vendable": null, 
    "Etat": null, 
    "DateCrt": null 
} 

index.php(API苗条框架):

<?php 

require 'vendor/autoload.php'; 
function connex(){ 
$serveur = "127.0.0.1"; 
$username = "root"; 
$password = ""; 
$db = "bms1"; 

$conn = new mysqli($serveur, $username, $password, $db); 
if ($conn->connect_error) { 
    die("Connection failed: " . $conn->connect_error); 
} 
return $conn; 
} 
$app = new Slim\App(); 


$app->get('/articles', function ($request, $response, $args) { 
    $con=connex(); 
    $new=""; 
    $sql = "SELECT * FROM bms_op_article"; 
    $result = $con->query($sql); 

    foreach($result as $ligne){ 
     $new.=$response->withJson($ligne); 
    } 
    return $new;  
}); 

$app->run(); 

?> 

回答

0

我完全不清楚你的问题是什么。

但是,您使用$response->withJson()不正确。 $response->withJson()需要一个关联数组和身体返回与JSON一个Response对象,因此应该这样使用:

$app->get('/test', function ($request, $response, $args) { 
    $dataToReturn = [ 
     "name": "Rob", 
     "email": "[email protected]", 
    ]; 
    return $response->withJson($data); 
}); 

目前还不清楚是什么$result在你的代码。如果它是一个关联数组,则可以直接将它传递给withJson()。如果不是,那么你需要迭代它并建立一个关联数组。