Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/200.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/ssl/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php JSON数组和Android列表视图不工作_Php_Android_Json_Arrays - Fatal编程技术网

Php JSON数组和Android列表视图不工作

Php JSON数组和Android列表视图不工作,php,android,json,arrays,Php,Android,Json,Arrays,好的,我有一个php脚本: <?php mysql_connect("localhost","root","root"); mysql_select_db("FYP"); $sql=mysql_query("select team_name, games_played, games_won, games_drawn, games_lost, goals_for, goals_against, goal_difference, current_points from T

好的,我有一个php脚本:

<?php
  mysql_connect("localhost","root","root");
  mysql_select_db("FYP");
  $sql=mysql_query("select team_name, games_played, games_won,
  games_drawn, games_lost, goals_for, goals_against, goal_difference, 
  current_points from Team where team_name='Man Utd'");
  while($row=mysql_fetch_assoc($sql)) $output[]=$row;
  print(json_encode($output));
  mysql_close();
?>
据我所知,这是一个JSON对象数组。但是,当我将此数组作为列表显示在我的应用程序上时,我正在编写的教程要求它是JSONObject而不是JSONArray。我试图修改代码以获取和数组,但我没有运气,有人能帮我吗?android tut是:

我遇到的主要问题是这行代码:

    JSONArray  earthquakes = json.getJSONArray("earthquakes");

显然,我的JSON不像示例那样具有元素标识符,当我删除该行时,它就不起作用了。任何帮助都会很棒,谢谢

虽然您的JSON结果字符串是合法的,但应用程序(如果按照教程建模)似乎希望有一个对象作为外部外壳,如下所示:

[
    {
        "team_name": "Man City",
        "games_played": "24",
        "games_won": "18",
        "games_drawn": "3",
        "games_lost": "3",
        "goals_for": "63",
        "goals_against": "19",
        "goal_difference": "44",
        "current_points": "57"
    },
    {
        "team_name": "Man Utd",
        "games_played": "24",
        "games_won": "17",
        "games_drawn": "4",
        "games_lost": "3",
        "goals_for": "59",
        "goals_against": "24",
        "goal_difference": "35",
        "current_points": "55"
    }
]
{ "teams":
[
    {
        "team_name": "Man City",
        "games_played": "24",
        "games_won": "18",
        "games_drawn": "3",
        "games_lost": "3",
        "goals_for": "63",
        "goals_against": "19",
        "goal_difference": "44",
        "current_points": "57"
    },
    {
        "team_name": "Man Utd",
        "games_played": "24",
        "games_won": "17",
        "games_drawn": "4",
        "games_lost": "3",
        "goals_for": "59",
        "goals_against": "24",
        "goal_difference": "35",
        "current_points": "55"
    }
]
}
这种格式(使用对象作为外壳)实际上在返回JSON/JSONP的web服务中非常常见

更新 根据,外部必须是一个对象。使用我在回答中建议的结构,您的行是:

JSONArray  teams = json.getJSONArray("teams");

只需通过添加一个顶层来修改输出:

print(json_encode(array('earthquakes' => $output)));