JSON数组和Android列表视图不工作
行,所以我有一个PHP脚本:
Ok so i have a php script:
<?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();
?>
这是我的查询DB和返回匹配查询的所有行等等等等,然后连接codeS它变成JSON,看起来像这样:
that queries my db and returns all the rows matching the query etc etc, then encodes it into JSON, that looks like this:
[
{
"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对象的数组。然而,当我把这个数组显示为我的应用程序列表,我工作过的教程需要它是一个JSONObject,而不是一个JSONArray。我试图修改code采取和数组,但我有没有运气,谁能帮助? Android的啧啧为:的http://p-xr.com/android-tutorial-how-to-parse-read-json-data-into-a-android-listview/
我遇到麻烦的主要部分是此行code的:
The main part i'm having trouble with is this line of code:
JSONArray earthquakes = json.getJSONArray("earthquakes");
显然,我的JSON不具备的元素标识符,如样品确实,当我删除该行这是行不通的。任何帮助将是辉煌的,谢谢!
Obviously my JSON doesn't have the element identifier like the sample does, and when i remove that line it doesn't work. Any help would be brilliant, Thanks!
当你的JSON结果字符串是合法的,似乎该应用程序(如果该教程为蓝本)期待一个对象作为外壳,这样的事情:
While your JSON result string is legit, it seems that the app (if modeled after the tutorial) is expecting an object as the outer enclosure, something like this:
{ "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"
}
]
}
(利用的一个对象作为外壳),这种格式实际上是$ Web服务之间的公共P $ ptty返回JSON / JSONP。
This format (of utilizing an object as the outer enclosure) is actually pretty common among web services that return JSON/JSONP.
更新
每这,外必须是一个对象。在我的答案使用推荐的结构,你的线路是:
Update Per this, the outer has to be an object. Using the recommended structure in my answer, your line would be:
JSONArray teams = json.getJSONArray("teams");