I have tried different methods to parse JSON file in Android but I'm getting errors in printing them out.
Here is my entire JSON file:
{
conferences: [
{
id: "7ab1c9c0-4ffa-4f27-bcb6-be54d6ca6127",
name: "EASTERN CONFERENCE",
alias: "EASTERN",
divisions: [
{
id: "1fad71d8-5b9e-4159-921b-9b98d0573f51",
name: "Atlantic",
alias: "ATLANTIC",
teams: [
{
id: "4417d3cb-0f24-11e2-8525-18a905767e44",
name: "Lightning",
market: "Tampa Bay",
reference: "14",
rank: {
division: 1,
conference: 1,
clinched: "conference"
}
},
{
id: "4416ba1a-0f24-11e2-8525-18a905767e44",
name: "Bruins",
market: "Boston",
reference: "6",
rank: {
division: 2,
conference: 2,
clinched: "playoff_spot"
}
},
]
},
{
id: "d5ec45c4-a691-43ac-9e6d-92de92e763e7",
name: "Metropolitan",
alias: "METROPOLITAN",
teams: [
{
id: "4417eede-0f24-11e2-8525-18a905767e44",
name: "Capitals",
market: "Washington",
reference: "15",
rank: {
division: 1,
conference: 3,
clinched: "division"
}
},
]
}
]
},
{
id: "64901512-9ca9-4bea-aa80-16dbcbdae230",
name: "WESTERN CONFERENCE",
alias: "WESTERN",
divisions: [
{
id: "5e868c4d-c6a3-4901-bc3c-3b7a4509e402",
name: "Central",
alias: "CENTRAL",
teams: [
{
id: "441643b7-0f24-11e2-8525-18a905767e44",
name: "Predators",
market: "Nashville",
reference: "18",
rank: {
division: 1,
conference: 1,
clinched: "presidents_trophy"
}
}
]
},
{
id: "17101b65-e8b9-4cda-a963-eea874aed81f",
name: "Pacific",
alias: "PACIFIC",
teams: [
{
id: "42376e1c-6da8-461e-9443-cfcf0a9fcc4d",
name: "Golden Knights",
market: "Vegas",
reference: "54",
rank: {
division: 1,
conference: 3,
clinched: "division"
}
},
]
}
]
}
]
}
Here is the code that I have implemented to make it work:
@Override
protected Void doInBackground(Void... arg0) {
HttpHandler sh = new HttpHandler();
// Making a request to url and getting response
String jsonStr = sh.makeServiceCall(url);
Log.e(TAG, "Response from url: " + jsonStr);
if (jsonStr != null) {
try {
JSONObject jsonObj = new JSONObject(jsonStr);
// Getting JSON Array node
JSONArray con = jsonObj.getJSONArray("conferences");
JSONArray division = con.getJSONArray("divisions");
JSONArray teams = jsonObj.getJSONArray("teams");
for (int i = 0; i < teams.length(); i++) {
JSONObject c = con.getJSONObject(i);
String alias = c.getString("alias");
JSONObject t = teams.getJSONObject(i);
String name = t.getString("name");
JSONObject rank = t.getJSONObject("rank");
String div = rank.getString("division");
// tmp hash map for single fixture
HashMap<String, String> fixture = new HashMap<>();
// adding each child node to HashMap key => value
// fixture.put("alias", alias);
fixture.put("name", name);
fixture.put("division", div);
// adding contact to contact list
fixtureList.add(fixture);
}
Here is the onPostExecute method:
@Override
protected void onPostExecute(Void aVoid) {
// Dismiss the progress dialog
if (pDialog.isShowing())
pDialog.dismiss();
ListAdapter adapter = new SimpleAdapter(
MainActivity.this, fixtureList,
R.layout.list_ranking_item, new String[]{/**"alias"**/ "name", "div"},
new int[]{/**R.id.ranking_name**/ R.id.ranking_team,
R.id.ranking_rank});
lv.setAdapter(adapter);
}
I have taken the help from this site to layout my functions: https://www.androidhive.info/2012/01/android-json-parsing-tutorial/