Skip to content Skip to sidebar Skip to footer

Android Adding Json Objects Into An Array

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 an

Solution 1:

From what you say, you seem like trying to build a JSONArray out of some JSONObjects. This might help:

publicvoidwriteJSON() {
    JSONObject user = newJSONObject();
    JSONObject user2;
    user2 = newJSONObject();
    try {
        user.put("dish_id", "1");
        user.put("dish_custom", "2");
        user.put("quantity", "2");
        user.put("shared", "2");

        user2.put("dish_id", "2");
        user2.put("dish_custom", "2");
        user2.put("quantity", "4");
        user2.put("shared", "3");
    } catch (JSONException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }

    JSONArray notebookUsers = newJSONArray();
    notebookUsers.put(user);
    notebookUsers.put(user2);
    System.out.println("the JSON ARRAY is"+notebookUsers);

Here 2 json objects are added to 1 JSONArray.

The output of the System.out.println will look something like this:

the JSON ARRAY is[{"shared":"2","dish_custom":"2","dish_id":"1","quantity":"2"},{"shared":"3","dish_custom":"2","dish_id":"2","quantity":"4"}]

And the string comparison you are using is not right.

if(matchDateTemp != matchDate1)

u cannot compare strings like that. u can use something like:

if(!(matchDateTemp.equals(matchDate1)))

Solution 2:

Best thing to parse JSON is to use GSON. What you have to do is

  • Download GSON jar and add to your build path
  • Now create classes that represent your JSON data structure
  • Parse the data in just one line. GSON will do that for you

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);
  • Now the MatchDetails class has all the FixtureLists you need
  • access the fixture array via matchDetails.getData().getFixtures();

the MatchDetails class

publicclassMatchDetails {

    //"code":200,"error":nullprivateString code;
    privateString error;
    privateFixtures data;
    /**
     * Gets the code.
     * 
     * @return <tt> the code.</tt>
     */publicStringgetCode() {
        return code;
    }
    /**
     * Sets the code.
     *
     * @param code <tt> the code to set.</tt>
     */publicvoidsetCode(String code) {
        this.code = code;
    }
    /**
     * Gets the error.
     * 
     * @return <tt> the error.</tt>
     */publicStringgetError() {
        return error;
    }
    /**
     * Sets the error.
     *
     * @param error <tt> the error to set.</tt>
     */publicvoidsetError(String error) {
        this.error = error;
    }
    /**
     * Gets the data.
     * 
     * @return <tt> the data.</tt>
     */publicFixturesgetData() {
        return data;
    }
    /**
     * Sets the data.
     *
     * @param data <tt> the data to set.</tt>
     */publicvoidsetData(Fixtures data) {
        this.data = data;
    }
    /* (non-Javadoc)
     * @see java.lang.Object#toString()
     */@OverridepublicStringtoString() {
        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<Fixture> 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;

/**
 * <br>
 * <div style="width:600px;text-align:justify;">
 *
 * TODO: Class comment.
 *
 * </div>
 * <br>
 * @author Sunil Kumar E
 *
 */publicclassFixtures {

    private List<Fixture> fixtures;

    /**
     * Gets the fixtures.
     * 
     * @return <tt> the fixtures.</tt>
     */public List<Fixture> getFixtures() {
        return fixtures;
    }

    /**
     * Sets the fixtures.
     *
     * @param fixtures <tt> the fixtures to set.</tt>
     */publicvoidsetFixtures(List<Fixture> fixtures) {
        this.fixtures = fixtures;
    }

    /* (non-Javadoc)
     * @see java.lang.Object#toString()
     */@Overridepublic String toString() {
        return"Fixtures [fixtures=" + fixtures + "]";
    }


    public List<Fixture> sortFixtures()
    {
        Collections.sort(getFixtures(), newComparator<Fixture>() {

            @Overridepublicintcompare(Fixture o1, Fixture o2) {
                SimpleDateFormatdf1=newSimpleDateFormat("yyyy-MM-dd");

                Datedate1=null;
                Datedate2=null;

                try {
                    date1 = df1 .parse(o1.getMatchdate());
                    date2 = df1 .parse(o2.getMatchdate());
                } catch (ParseException e) {
                    e.printStackTrace();
                }
                if(date1==null || date2==null) return0;

                if (date1.after(date2))
                    return1;
                elseif(date1.before(date2)) 
                    return -1;
                return0;
            }
        });

        return getFixtures();
    }
}

the Fixture class

publicclassFixture {
//"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"privateString kickoff;
    privateString matchdate;
    privateString homescore;
    privateString awayscore;
    privateString attendance;
    privateString homepens;
    privateString awaypens;
    privateString division_id;
    privateString division;
    privateString comp;
    privateString location;
    privateString fixture_note;
    privateString hometeam_id;
    privateString hometeam;
    privateString awayteam_id;
    privateString awayteam;
    /**
     * Gets the kickoff.
     * 
     * @return <tt> the kickoff.</tt>
     */publicStringgetKickoff() {
        return kickoff;
    }
    /**
     * Sets the kickoff.
     *
     * @param kickoff <tt> the kickoff to set.</tt>
     */publicvoidsetKickoff(String kickoff) {
        this.kickoff = kickoff;
    }
    /**
     * Gets the matchdate.
     * 
     * @return <tt> the matchdate.</tt>
     */publicStringgetMatchdate() {
        return matchdate;
    }
    /**
     * Sets the matchdate.
     *
     * @param matchdate <tt> the matchdate to set.</tt>
     */publicvoidsetMatchdate(String matchdate) {
        this.matchdate = matchdate;
    }
    /**
     * Gets the homescore.
     * 
     * @return <tt> the homescore.</tt>
     */publicStringgetHomescore() {
        return homescore;
    }
    /**
     * Sets the homescore.
     *
     * @param homescore <tt> the homescore to set.</tt>
     */publicvoidsetHomescore(String homescore) {
        this.homescore = homescore;
    }
    /**
     * Gets the awayscore.
     * 
     * @return <tt> the awayscore.</tt>
     */publicStringgetAwayscore() {
        return awayscore;
    }
    /**
     * Sets the awayscore.
     *
     * @param awayscore <tt> the awayscore to set.</tt>
     */publicvoidsetAwayscore(String awayscore) {
        this.awayscore = awayscore;
    }
    /**
     * Gets the attendance.
     * 
     * @return <tt> the attendance.</tt>
     */publicStringgetAttendance() {
        return attendance;
    }
    /**
     * Sets the attendance.
     *
     * @param attendance <tt> the attendance to set.</tt>
     */publicvoidsetAttendance(String attendance) {
        this.attendance = attendance;
    }
    /**
     * Gets the homepens.
     * 
     * @return <tt> the homepens.</tt>
     */publicStringgetHomepens() {
        return homepens;
    }
    /**
     * Sets the homepens.
     *
     * @param homepens <tt> the homepens to set.</tt>
     */publicvoidsetHomepens(String homepens) {
        this.homepens = homepens;
    }
    /**
     * Gets the awaypens.
     * 
     * @return <tt> the awaypens.</tt>
     */publicStringgetAwaypens() {
        return awaypens;
    }
    /**
     * Sets the awaypens.
     *
     * @param awaypens <tt> the awaypens to set.</tt>
     */publicvoidsetAwaypens(String awaypens) {
        this.awaypens = awaypens;
    }
    /**
     * Gets the division_id.
     * 
     * @return <tt> the division_id.</tt>
     */publicStringgetDivision_id() {
        return division_id;
    }
    /**
     * Sets the division_id.
     *
     * @param division_id <tt> the division_id to set.</tt>
     */publicvoidsetDivision_id(String division_id) {
        this.division_id = division_id;
    }
    /**
     * Gets the division.
     * 
     * @return <tt> the division.</tt>
     */publicStringgetDivision() {
        return division;
    }
    /**
     * Sets the division.
     *
     * @param division <tt> the division to set.</tt>
     */publicvoidsetDivision(String division) {
        this.division = division;
    }
    /**
     * Gets the comp.
     * 
     * @return <tt> the comp.</tt>
     */publicStringgetComp() {
        return comp;
    }
    /**
     * Sets the comp.
     *
     * @param comp <tt> the comp to set.</tt>
     */publicvoidsetComp(String comp) {
        this.comp = comp;
    }
    /**
     * Gets the location.
     * 
     * @return <tt> the location.</tt>
     */publicStringgetLocation() {
        return location;
    }
    /**
     * Sets the location.
     *
     * @param location <tt> the location to set.</tt>
     */publicvoidsetLocation(String location) {
        this.location = location;
    }
    /**
     * Gets the fixture_note.
     * 
     * @return <tt> the fixture_note.</tt>
     */publicStringgetFixture_note() {
        return fixture_note;
    }
    /**
     * Sets the fixture_note.
     *
     * @param fixture_note <tt> the fixture_note to set.</tt>
     */publicvoidsetFixture_note(String fixture_note) {
        this.fixture_note = fixture_note;
    }
    /**
     * Gets the hometeam_id.
     * 
     * @return <tt> the hometeam_id.</tt>
     */publicStringgetHometeam_id() {
        return hometeam_id;
    }
    /**
     * Sets the hometeam_id.
     *
     * @param hometeam_id <tt> the hometeam_id to set.</tt>
     */publicvoidsetHometeam_id(String hometeam_id) {
        this.hometeam_id = hometeam_id;
    }
    /**
     * Gets the hometeam.
     * 
     * @return <tt> the hometeam.</tt>
     */publicStringgetHometeam() {
        return hometeam;
    }
    /**
     * Sets the hometeam.
     *
     * @param hometeam <tt> the hometeam to set.</tt>
     */publicvoidsetHometeam(String hometeam) {
        this.hometeam = hometeam;
    }
    /**
     * Gets the awayteam_id.
     * 
     * @return <tt> the awayteam_id.</tt>
     */publicStringgetAwayteam_id() {
        return awayteam_id;
    }
    /**
     * Sets the awayteam_id.
     *
     * @param awayteam_id <tt> the awayteam_id to set.</tt>
     */publicvoidsetAwayteam_id(String awayteam_id) {
        this.awayteam_id = awayteam_id;
    }
    /**
     * Gets the awayteam.
     * 
     * @return <tt> the awayteam.</tt>
     */publicStringgetAwayteam() {
        return awayteam;
    }
    /**
     * Sets the awayteam.
     *
     * @param awayteam <tt> the awayteam to set.</tt>
     */publicvoidsetAwayteam(String awayteam) {
        this.awayteam = awayteam;
    }
    /* (non-Javadoc)
     * @see java.lang.Object#toString()
     */@OverridepublicStringtoString() {
        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
                + "]";
    }
}

Post a Comment for "Android Adding Json Objects Into An Array"