【发布时间】:2013-05-12 02:15:13
【问题描述】:
我正在开发一个 Android 应用,对于 API,我将我的请求发送到应该返回 JSON 数据的 URL。
这是我在输出中得到的:
我希望它显示为 Twitter 响应:
我假设我的响应没有被 JSON Formatter Chrome 扩展解析,因为它的编码错误,因此我的应用无法获得我需要的值。
这是我的 PHP 代码:
<?php
$response = array();
if (isset($_POST['name']) && isset($_POST['price']) && isset($_POST['description']))
{
$name = $_POST['name'];
$price = $_POST['price'];
$description = $_POST['decription'];
require_once __DIR__ . '/db_connect.php';
$db = new DB_CONNECT();
$result = mysql_query("INSER INTO products(name, price, description) VALUES('$name', '$price', '$description')");
if ($result) {
$response["success"] = 1;
$response["message"] = "Product successfully created.";
echo json_encode($response);
} else {
$response["success"] = 0;
$response["message"] = "Oops! An error occurred!";
echo json_encode($response);
}
} else {
$response["success"] = 0;
$response["message"] = "Required field(s) is missing";
echo json_encode($response);
}
?>
我想知道如何正确显示 JSON 数据,以便 JSON Formatter 和我的 android App 可以正确解析它。
【问题讨论】:
-
可能是错字,但您的插入查询以
INSER开头。 -
如果您发布您的实际结果 JSON,而不是图片,这将更加有用。
-
我对 PHP 的 JSON 输出不是很熟悉,但我很好奇我可以在你的第二张截图中看到一个“原始”和“解析”函数,这不是一些 javascript 函数吗?实际上是格式化输出吗?所以说你做了一个漂亮的打印,那不会增加你的页面数据大小并且它不应该是用户想要的吗?
-
@ChorWaiChun 这就是 JSON Formatter Chrome 扩展的实际应用。