【问题标题】:Json Array from PHP MySQL [closed]来自 PHP MySQL 的 Json 数组 [关闭]
【发布时间】:2013-12-18 14:04:47
【问题描述】:

我正在以这种形式从我的 android 应用程序的 php 文件中获取我的 json 响应

[{"countryname":"India","flag":"http:\/\/india.in\/india.png"}]

我需要这种形式,请给我一些帮助。

{"countries":[{"countryname":"India","flag":"http:\/\/india.in\/india.png"}]}

【问题讨论】:

  • 第一个 JSON 字符串是如何转换成第二个的?它只是随机的吗? countries 是从哪里来的?
  • 我说过我需要这种(第二种)形式
  • 你不能修改 PHP 吗?
  • @Nfear 对不起,但我是 php 新手,所以你能告诉我这段代码 $sql=mysql_query("select * from t1 ORDER BY timestamp DESC");而($row=mysql_fetch_assoc($sql)) $output[]=$row; json_encode($输出);打印(json_encode($output));

标签: php android json


【解决方案1】:

你在 php 中有你的数组,你把它编码成 json。现在您需要像这样添加另一个数组级别:

$country = array('countryname' -> 'India', ...);
$countries = array('countries' => $country );
echo json_encode($countries);

这只是我的猜测,因为您没有显示任何代码。

更新

$sql=mysql_query("select * from t1 ORDER BY timestamp DESC");
while($row=mysql_fetch_assoc($sql)) {
    $output[]=$row;
}
// json_encode($output); doesn't do anything useful

$countries = array('countries' => $output);

echo json_encode($countries);

【讨论】:

  • 抱歉,我是 php 新手,请告诉我这段代码 $sql=mysql_query("select * from t1 ORDER BY timestamp DESC");而($row=mysql_fetch_assoc($sql)) $output[]=$row; json_encode($输出);打印(json_encode($output));
  • 非常感谢...您使我的应用程序正常工作...请让我知道是否可以为您提供帮助。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2013-02-26
  • 2021-10-28
  • 2013-11-29
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多