Android将Json对象添加到Array中

Luk*_*ley 2 android json

我要做的是创建一个JSONObject,其中包含一个由String组织的其他JSONObject数组?例如,我想创建一个JSONObject,其中包含一个对象数组,这些对象包含也按匹配日期组织的灯具

为您提供我想要实现的目标的视觉参考

JSONObject (JSONArray("Object 10/10/12" {(fixtures content)(fixtures content)}")
                     ("Object 11/10/12" {(fixtures content)(fixtures content)}"))
Run Code Online (Sandbox Code Playgroud)

继续我到目前为止所尝试的但是却无法让它发挥作用

        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();
            }
Run Code Online (Sandbox Code Playgroud)

继承人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"
        }]
    }
}
Run Code Online (Sandbox Code Playgroud)

pix*_*een 14

从你说的话来看,你似乎试图建立JSONArray一些JSONObjects.这可能有所帮助:

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);
Run Code Online (Sandbox Code Playgroud)

这里有2个json对象被添加到1个JSONArray中.

System.out.println将看起来像这样的输出:

the JSON ARRAY is[{"shared":"2","dish_custom":"2","dish_id":"1","quantity":"2"},{"shared":"3","dish_custom":"2","dish_id":"2","quantity":"4"}]
Run Code Online (Sandbox Code Playgroud)

你正在使用的字符串比较是不对的.

if(matchDateTemp != matchDate1)
Run Code Online (Sandbox Code Playgroud)

你不能比较那样的字符串.你可以使用类似的东西:

if(!(matchDateTemp.equals(matchDate1)))
Run Code Online (Sandbox Code Playgroud)