What I\'m trying to do is create a JSONObject that contains an Array of other JSONObjects that are organized by a String? for example i want to create a JSONObject that contains
Best thing to parse JSON is to use GSON. What you have to do is
parsing in just one line
String json = "{\"code\":200,\"error\":null,\"data\":{\"fixtures\":[{\"kickoff\":\"15:00:00\",\"matchdate\":\"2012-07-14\",\"homescore\":null,\"awayscore\":null,\"attendance\":null,\"homepens\":null,\"awaypens\":null,\"division_id\":\"5059\",\"division\":\"Testing 1\",\"comp\":\"LGE\",\"location\":null,\"fixture_note\":null,\"hometeam_id\":\"64930\",\"hometeam\":\"Team 1\",\"awayteam_id\":\"64933\",\"awayteam\":\"Team 4\"},{\"kickoff\":\"15:00:00\",\"matchdate\":\"2012-07-14\",\"homescore\":null,\"awayscore\":null,\"attendance\":null,\"homepens\":null,\"awaypens\":null,\"division_id\":\"5059\",\"division\":\"Testing 1\",\"comp\":\"LGE\",\"location\":null,\"fixture_note\":null,\"hometeam_id\":\"64935\",\"hometeam\":\"Team 6\",\"awayteam_id\":\"64937\",\"awayteam\":\"Team 8\"},{\"kickoff\":\"15:00:00\",\"matchdate\":\"2012-07-28\",\"homescore\":null,\"awayscore\":null,\"attendance\":null,\"homepens\":null,\"awaypens\":null,\"division_id\":\"5059\",\"division\":\"Testing 1\",\"comp\":\"LGE\",\"location\":null,\"fixture_note\":null,\"hometeam_id\":\"64930\",\"hometeam\":\"Team 1\",\"awayteam_id\":\"64931\",\"awayteam\":\"Team 2\"},{\"kickoff\":\"15:00:00\",\"matchdate\":\"2012-07-28\",\"homescore\":null,\"awayscore\":null,\"attendance\":null,\"homepens\":null,\"awaypens\":null,\"division_id\":\"5059\",\"division\":\"Testing 1\",\"comp\":\"LGE\",\"location\":null,\"fixture_note\":null,\"hometeam_id\":\"64930\",\"hometeam\":\"Team 1\",\"awayteam_id\":\"64931\",\"awayteam\":\"Team 2\"}]}}";
MatchDetails matchDetails=new Gson().fromJson(json,MatchDetails.class);
System.out.println(matchDetails);
MatchDetails
class has all the Fixture
Lists
you needmatchDetails.getData().getFixtures();
the MatchDetails
class
public class MatchDetails {
//"code":200,"error":null
private String code;
private String error;
private Fixtures data;
/**
* Gets the code.
*
* @return the code.
*/
public String getCode() {
return code;
}
/**
* Sets the code.
*
* @param code the code to set.
*/
public void setCode(String code) {
this.code = code;
}
/**
* Gets the error.
*
* @return the error.
*/
public String getError() {
return error;
}
/**
* Sets the error.
*
* @param error the error to set.
*/
public void setError(String error) {
this.error = error;
}
/**
* Gets the data.
*
* @return the data.
*/
public Fixtures getData() {
return data;
}
/**
* Sets the data.
*
* @param data the data to set.
*/
public void setData(Fixtures data) {
this.data = data;
}
/* (non-Javadoc)
* @see java.lang.Object#toString()
*/
@Override
public String toString() {
return "MatchDetails [code=" + code + ", error=" + error + ", data="
+ data + "]";
}
}
EDIT
edited Fixtures
class with sortFixtures
function. Use it as below
String json = "{\"code\":200,\"error\":null,\"data\":{\"fixtures\":[{\"kickoff\":\"15:00:00\",\"matchdate\":\"2012-05-14\",\"homescore\":null,\"awayscore\":null,\"attendance\":null,\"homepens\":null,\"awaypens\":null,\"division_id\":\"5059\",\"division\":\"Testing 1\",\"comp\":\"LGE\",\"location\":null,\"fixture_note\":null,\"hometeam_id\":\"64930\",\"hometeam\":\"Team 1\",\"awayteam_id\":\"64933\",\"awayteam\":\"Team 4\"},{\"kickoff\":\"15:00:00\",\"matchdate\":\"2012-07-14\",\"homescore\":null,\"awayscore\":null,\"attendance\":null,\"homepens\":null,\"awaypens\":null,\"division_id\":\"5059\",\"division\":\"Testing 1\",\"comp\":\"LGE\",\"location\":null,\"fixture_note\":null,\"hometeam_id\":\"64935\",\"hometeam\":\"Team 6\",\"awayteam_id\":\"64937\",\"awayteam\":\"Team 8\"},{\"kickoff\":\"15:00:00\",\"matchdate\":\"2012-12-28\",\"homescore\":null,\"awayscore\":null,\"attendance\":null,\"homepens\":null,\"awaypens\":null,\"division_id\":\"5059\",\"division\":\"Testing 1\",\"comp\":\"LGE\",\"location\":null,\"fixture_note\":null,\"hometeam_id\":\"64930\",\"hometeam\":\"Team 1\",\"awayteam_id\":\"64931\",\"awayteam\":\"Team 2\"},{\"kickoff\":\"15:00:00\",\"matchdate\":\"2012-01-28\",\"homescore\":null,\"awayscore\":null,\"attendance\":null,\"homepens\":null,\"awaypens\":null,\"division_id\":\"5059\",\"division\":\"Testing 1\",\"comp\":\"LGE\",\"location\":null,\"fixture_note\":null,\"hometeam_id\":\"64930\",\"hometeam\":\"Team 1\",\"awayteam_id\":\"64931\",\"awayteam\":\"Team 2\"}]}}";
MatchDetails matchDetails=new Gson().fromJson(json,MatchDetails.class);
System.out.println(matchDetails);
System.out.println("Unsorted");
Fixtures fixturesObj = matchDetails.getData();
List fixtures = fixturesObj.getFixtures();
for (Fixture fixture : fixtures) {
System.out.println(fixture);
}
System.out.println("Sorted");
fixtures = fixturesObj.sortFixtures();
for (Fixture fixture : fixtures) {
System.out.println(fixture);
}
The Fixtures
class
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Collections;
import java.util.Comparator;
import java.util.Date;
import java.util.List;
/**
*
*
*
* TODO: Class comment.
*
*
*
* @author Sunil Kumar E
*
*/
public class Fixtures {
private List fixtures;
/**
* Gets the fixtures.
*
* @return the fixtures.
*/
public List getFixtures() {
return fixtures;
}
/**
* Sets the fixtures.
*
* @param fixtures the fixtures to set.
*/
public void setFixtures(List fixtures) {
this.fixtures = fixtures;
}
/* (non-Javadoc)
* @see java.lang.Object#toString()
*/
@Override
public String toString() {
return "Fixtures [fixtures=" + fixtures + "]";
}
public List sortFixtures()
{
Collections.sort(getFixtures(), new Comparator() {
@Override
public int compare(Fixture o1, Fixture o2) {
SimpleDateFormat df1 = new SimpleDateFormat("yyyy-MM-dd");
Date date1 = null;
Date date2 = null;
try {
date1 = df1 .parse(o1.getMatchdate());
date2 = df1 .parse(o2.getMatchdate());
} catch (ParseException e) {
e.printStackTrace();
}
if(date1==null || date2==null) return 0;
if (date1.after(date2))
return 1;
else if(date1.before(date2))
return -1;
return 0;
}
});
return getFixtures();
}
}
the Fixture
class
public class Fixture {
//"kickoff":"15:00:00","matchdate":"2012-07-14","homescore":null,
// "awayscore":null,"attendance":null,"homepens":null,"awaypens":null,
// "division_id":"5059","division":"Testing 1","comp":"LGE","location":null,
// "fixture_note":null,"hometeam_id":"64930","hometeam":"Team 1",
// "awayteam_id":"64933","awayteam":"Team 4"
private String kickoff;
private String matchdate;
private String homescore;
private String awayscore;
private String attendance;
private String homepens;
private String awaypens;
private String division_id;
private String division;
private String comp;
private String location;
private String fixture_note;
private String hometeam_id;
private String hometeam;
private String awayteam_id;
private String awayteam;
/**
* Gets the kickoff.
*
* @return the kickoff.
*/
public String getKickoff() {
return kickoff;
}
/**
* Sets the kickoff.
*
* @param kickoff the kickoff to set.
*/
public void setKickoff(String kickoff) {
this.kickoff = kickoff;
}
/**
* Gets the matchdate.
*
* @return the matchdate.
*/
public String getMatchdate() {
return matchdate;
}
/**
* Sets the matchdate.
*
* @param matchdate the matchdate to set.
*/
public void setMatchdate(String matchdate) {
this.matchdate = matchdate;
}
/**
* Gets the homescore.
*
* @return the homescore.
*/
public String getHomescore() {
return homescore;
}
/**
* Sets the homescore.
*
* @param homescore the homescore to set.
*/
public void setHomescore(String homescore) {
this.homescore = homescore;
}
/**
* Gets the awayscore.
*
* @return the awayscore.
*/
public String getAwayscore() {
return awayscore;
}
/**
* Sets the awayscore.
*
* @param awayscore the awayscore to set.
*/
public void setAwayscore(String awayscore) {
this.awayscore = awayscore;
}
/**
* Gets the attendance.
*
* @return the attendance.
*/
public String getAttendance() {
return attendance;
}
/**
* Sets the attendance.
*
* @param attendance the attendance to set.
*/
public void setAttendance(String attendance) {
this.attendance = attendance;
}
/**
* Gets the homepens.
*
* @return the homepens.
*/
public String getHomepens() {
return homepens;
}
/**
* Sets the homepens.
*
* @param homepens the homepens to set.
*/
public void setHomepens(String homepens) {
this.homepens = homepens;
}
/**
* Gets the awaypens.
*
* @return the awaypens.
*/
public String getAwaypens() {
return awaypens;
}
/**
* Sets the awaypens.
*
* @param awaypens the awaypens to set.
*/
public void setAwaypens(String awaypens) {
this.awaypens = awaypens;
}
/**
* Gets the division_id.
*
* @return the division_id.
*/
public String getDivision_id() {
return division_id;
}
/**
* Sets the division_id.
*
* @param division_id the division_id to set.
*/
public void setDivision_id(String division_id) {
this.division_id = division_id;
}
/**
* Gets the division.
*
* @return the division.
*/
public String getDivision() {
return division;
}
/**
* Sets the division.
*
* @param division the division to set.
*/
public void setDivision(String division) {
this.division = division;
}
/**
* Gets the comp.
*
* @return the comp.
*/
public String getComp() {
return comp;
}
/**
* Sets the comp.
*
* @param comp the comp to set.
*/
public void setComp(String comp) {
this.comp = comp;
}
/**
* Gets the location.
*
* @return the location.
*/
public String getLocation() {
return location;
}
/**
* Sets the location.
*
* @param location the location to set.
*/
public void setLocation(String location) {
this.location = location;
}
/**
* Gets the fixture_note.
*
* @return the fixture_note.
*/
public String getFixture_note() {
return fixture_note;
}
/**
* Sets the fixture_note.
*
* @param fixture_note the fixture_note to set.
*/
public void setFixture_note(String fixture_note) {
this.fixture_note = fixture_note;
}
/**
* Gets the hometeam_id.
*
* @return the hometeam_id.
*/
public String getHometeam_id() {
return hometeam_id;
}
/**
* Sets the hometeam_id.
*
* @param hometeam_id the hometeam_id to set.
*/
public void setHometeam_id(String hometeam_id) {
this.hometeam_id = hometeam_id;
}
/**
* Gets the hometeam.
*
* @return the hometeam.
*/
public String getHometeam() {
return hometeam;
}
/**
* Sets the hometeam.
*
* @param hometeam the hometeam to set.
*/
public void setHometeam(String hometeam) {
this.hometeam = hometeam;
}
/**
* Gets the awayteam_id.
*
* @return the awayteam_id.
*/
public String getAwayteam_id() {
return awayteam_id;
}
/**
* Sets the awayteam_id.
*
* @param awayteam_id the awayteam_id to set.
*/
public void setAwayteam_id(String awayteam_id) {
this.awayteam_id = awayteam_id;
}
/**
* Gets the awayteam.
*
* @return the awayteam.
*/
public String getAwayteam() {
return awayteam;
}
/**
* Sets the awayteam.
*
* @param awayteam the awayteam to set.
*/
public void setAwayteam(String awayteam) {
this.awayteam = awayteam;
}
/* (non-Javadoc)
* @see java.lang.Object#toString()
*/
@Override
public String toString() {
return "Fixture [kickoff=" + kickoff + ", matchdate=" + matchdate
+ ", homescore=" + homescore + ", awayscore=" + awayscore
+ ", attendance=" + attendance + ", homepens=" + homepens
+ ", awaypens=" + awaypens + ", division_id=" + division_id
+ ", division=" + division + ", comp=" + comp + ", location="
+ location + ", fixture_note=" + fixture_note
+ ", hometeam_id=" + hometeam_id + ", hometeam=" + hometeam
+ ", awayteam_id=" + awayteam_id + ", awayteam=" + awayteam
+ "]";
}
}