Luke Batley Luke Batley - 5 months ago 8
JSON Question

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 array of objects that contain fixtures that are also organized by match date

to give you a visual reference of what i am trying to acheive

JSONObject (JSONArray("Object 10/10/12" {(fixtures content)(fixtures content)}")
("Object 11/10/12" {(fixtures content)(fixtures content)}"))


heres what i have tried so far but just can't get it to work

String matchDate1 = null;
JSONArray datesArray = null;
JSONObject fixturesInfo = null;
JSONArray fixturesInfoArray = null;
String matchDateTemp = null;

for(int f = 0; f < fixturesArray.length(); f++){

JSONObject matchDateDict = fixturesArray.getJSONObject(f);
matchDate1 = matchDateDict.getString("matchdate");
JSONArray fixturesInfoDict = fixturesInfo.getJSONArray(matchDate1);

if(fixturesInfoDict == null){
tempArray = null;
} else {
tempArray = fixturesInfoDict;
}

if(matchDateTemp != matchDate1){
fixturesInfoArray.put(matchDate1);
}

matchDateTemp = matchDate1;

tempArray.put(fixturesArray.getJSONObject(f));
fixturesInfo.put(matchDate1, tempArray);

}




Log.v("MyFix", "fixturesInfo = " + fixturesInfo);

}catch (JSONException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}


heres the json feed

{
"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"
}]
}
}

Answer

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

public void writeJSON() {
    JSONObject user = new JSONObject();
    JSONObject user2;
    user2 = new JSONObject();
    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 = new JSONArray();
    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)))
Comments