将此PHP脚本的返回类型更改为JSONArray

时间:2014-11-08 00:40:49

标签: php json

感谢@michael协助让我的PHP脚本正常工作。我在Android应用中遇到了类型冲突,我想调整此脚本以返回JSONArray而不是JSONObject。这是脚本。

<?php

/*
* Following code will get all agencies matching the query
* Returns essential details
* An agency is identified by agency id
*/

require("DB_Link.php");

//query database for matching agency
$query = "SELECT * FROM agency WHERE City = :city";

$city = ($_GET['City']);

//Execute query
try {
    $stmt = $db->prepare("SELECT * FROM agency WHERE City = :city");
    $stmt->execute(array('city' => $city));
}
catch (PDOException $ex)    {
    $response["success"] = 0;
    $response["message"] = "Database Error!";
    die('Error!: ' . json_encode($ex->getMessage()));;
}

//Retrieve all found rows and add to array
$rows = $stmt->FETCHALL();


if($rows)   {
    $response["success"] = 1;
    $response["message"] = "Results Available!";
    $response["agencys"] = array();

    foreach ($rows as $row) {
        $agency         = array();
        $agency["AgencyID"] = $row["AgencyID"];
        $agency["AgencyName"]   = $row["AgencyName"];
        $agency["Address1"] = $row["Address1"];
        $agency["City"]     = $row["City"];
        $agency["State"]    = $row["State"];
        $agency["Zip"]      = $row["Zip"];
        $agency["Lat"]      = $row["Lat"];
        $agency["Lng"]      = $row["Lng"];

        //update response JSON data
        array_push($response["agencys"], $agency);
    }

    //Echo JSON response
    echo json_encode($response);

} else  {
    $response["success"] = 0;
    $response["message"] = "No Agency found!";
    die(json_encode($response));
}

?>

2 个答案:

答案 0 :(得分:1)

仅为后面的其他人介绍并解释为什么会发生这种情况。当PHP运行json_encode函数时,作为键值对的数组被视为对象,如果您希望它们作为对象数组返回,则需要将它们包装在另一个数组中。

json_encode(array("key" => "value"));

输出:

{"key":"value"}

把它变成一个数组......

json_encode(array(array("key" => "value")));

输出:

[{"key":"value"}]

答案 1 :(得分:0)

在这里玩了一些游戏后,我就想到了什么。将原始脚本更改为以下内容会返回我正在寻找的格式。

[{&#34; AgencyID&#34;:&#34; 2579&#34;&#34; AgencyName&#34;:&#34;名称&#34;&#34;地址1&#34 ;: &#34;街道&#34;&#34;市&#34;:&#34;城市&#34;&#34;国&#34;:&#34; NY&#34;&#34 ;邮编及#34;:&#34; 14226&#34;&#34;纬度&#34;:&#34; 0.000000&#34;&#34; LNG&#34;:&#34; 0.000000&# 34;}]

<?php



/*

* Following code will get all agencies matching the query

* Returns essential details

* An agency is identified by agency id

*/



require("DB_Link.php");



//query database for matching agency

$query = "SELECT * FROM agency WHERE City = :city";



$city = ($_GET['City']);



//Execute query

try {

    $stmt = $db->prepare("SELECT AgencyID, AgencyName, Address1, City, State, Zip, Lat, Lng FROM agency WHERE City = :city");

    $stmt->execute(array('city' => $city));

}

catch (PDOException $ex)    {

    $response["success"] = 0;

    $response["message"] = "Database Error!";

    die('Error!: ' . json_encode($ex->getMessage()));;

}



//Retrieve all found rows and add to array

$result = $stmt->FETCHALL(PDO::FETCH_ASSOC);

echo json_encode($result);


?>