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();
?>
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"
}
]
which to my knowledge, is an array of JSON objects. However when i take this array to display as a list on my app, the tutorial i’m working off requires it to be a JSONObject rather than a JSONArray. I have tried to modify the code to take and array but i have had no luck, can anyone help? The android tut is: http://p-xr.com/android-tutorial-how-to-parse-read-json-data-into-a-android-listview/
The main part i’m having trouble with is this line of code:
JSONArray earthquakes = json.getJSONArray("earthquakes");
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!
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:
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: