【问题标题】:Parsing JSON on android from slim micro framework API从苗条的微框架 API 在 android 上解析 JSON
【发布时间】:2017-07-25 08:05:56
【问题描述】:

如何解析这种类型的 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 slim框架):

<?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();

?>

【问题讨论】:

    标签: java android json parsing slim


    【解决方案1】:

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

    但是,您对$response-&gt;withJson() 的使用不正确。 $response-&gt;withJson() 接受一个关联数组并返回一个在正文中带有 JSON 的 Response 对象,因此应该像这样使用:

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

    不清楚您的代码中的 $result 是什么。如果是关联数组,可以直接传给withJson()。如果不是,则需要对其进行迭代并设置关联数组。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-11-22
      • 1970-01-01
      • 1970-01-01
      • 2016-03-28
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多