[英]How to get a JSON response using PHP?
我正在學習創建一個API以為我的Android應用返回JSON。 http://www.androidhive.info/2012/05/how-to-connect-android-with-php-mysql/
如果添加此行,則無法獲取JSON響應
$product["Area"] = $row["Area"];
如果我從下面的文件中刪除上述行。 然后我得到了回應。
我什至檢查了數據庫。 這就是我的數據庫的外觀,就像http://i.imgur.com/Cr4VmGW.png一樣。我不知道為什么會這樣。
提前致謝
這是我的PHP文件 (get_all_products)
<?php
/*
* Following code will list all the products
*/
// array for JSON response
$response = array();
// include db connect class
require_once __DIR__ . '/db_connect.php';
// connecting to db
$db = new DB_CONNECT();
// get all products from products table
$result = mysql_query("SELECT * FROM shelter") or die(mysql_error());
// check for empty result
if (mysql_num_rows($result) > 0) {
// looping through all results
// products node
$response["products"] = array();
while ($row = mysql_fetch_array($result)) {
// temp user array
$product = array();
$product["ID"] = $row["ID"];
$product["Timestamp"] = $row["Timestamp"];
$product["Accomodation"] = $row["Accomodation"];
$product["Area"] = $row["Area"];
// push single product into final response array
array_push($response["products"], $product);
}
// success
$response["success"] = 1;
// echoing JSON response
echo json_encode($response);
} else {
// no products found
$response["success"] = 0;
$response["message"] = "No products found";
// echo no users JSON
echo json_encode($response);
}
?>
json_encode()適用於UTF8字符串,可能是$ row [“ Area”]具有另一個編碼。 您可以使用utf8_encode函數(如果數據為ISO-8859)或iconv對其進行轉換。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.