fireblade
fireblade

Reputation: 25

Java JSON write jsonarray loop

I am trying to write to a json file using the following code:

    JSONObject obj = new JSONObject();
    JSONArray dlist = new JSONArray();
    JSONObject data = new JSONObject();
    //Data
    data.put("path", gamePath);
    data.put("lastprofile", profileList.getSelectedValue());
    dlist.add(data);

    obj.put("data", dlist);

    //Profiles
    JSONArray profileList = new JSONArray(); //profiles list
    JSONObject profileListObj = new JSONObject(); //profiles
    JSONArray profileDataList = new JSONArray(); //profile data list
    JSONObject profileData = new JSONObject(); //profile data

    //We now have to cycle every profile and create the data, then add it to the list.
    for(int i=profiles.size()-1; i>=0; i--) {
        Profile p = profiles.get(i);
        profileData.put("name", p.name);
        profileData.put("mods", p.mods.toString());
        System.out.println(p.name + "..." + p.mods.toString());
        profileDataList.add(profileData);
        System.out.println("list.." + profileDataList.toString());
        profileListObj.put("profile"+i, profileDataList);
        //obj.put("profile"+i, profileDataList);

        //profileList.add(profileListObj);
        //profileListObj.clear();
        //profileDataList.clear();
        //profileData.clear();
    }
    //profileList.add(profileListObj);
    obj.put("profiles", profileDataList);

What the problem is, And you can see some of my commented out lines i have tinkered around with. Is that the entire array will end up saving as the last object profiles.

What i am trying to have is:

profiles 
-profile1
--data
--data
-profile2
--data
--data etc, 

Here is what it produces using the example code above.

{
    "data" : [{
            "path" : "tempPath",
            "lastprofile" : "Profile1"
        }
    ],
    "profiles" : [{
            "name" : "Profile1", //This one is correct
            "mods" : "[base, mcconfig, mcconfig-startbonus]"
        }, {
            "name" : "Profile1", //This one should be profile2
            "mods" : "[base, mcconfig, mcconfig-startbonus]" //different mods...
        }, {
            "name" : "Profile1", //this one should be profile3
            "mods" : "[base, mcconfig, mcconfig-startbonus]" //different mods...
            }, {
            "name" : "Profile1",
            "mods" : "[base, mcconfig, mcconfig-startbonus]"
        }, {
            "name" : "Profile1",
            "mods" : "[base, mcconfig, mcconfig-startbonus]"
        }
    ]
}

I have tinkered around with multiple of the commented out lines, Getting as far as having all the data correct, just not organized in profiles. I am trying to get this to be as clean as possible so it is organized.

What i have come to the conclusion is, I am unable to put something into profileData which has the same key. So it re-adds the first input to profileDataList and continues with every loop after that.

Just some more information: Every profileData name and mods have a different string. There are 5 different profiles. and the profiles should be named profile with the corresponding number after them, Inside of profiles.

Upvotes: 1

Views: 765

Answers (1)

Salem
Salem

Reputation: 12986

When you do profileData.put("name", p.name); you are overwriting the same object, so in the final you will have an array with 3 references to the same object. To fix it create a new instance inside the loop (see comments):

for(int i=profiles.size()-1; i>=0; i--) {
    Profile p = profiles.get(i);
    profileData = new JsonObject(); // <- create a new object in each iteration
    profileData.put("name", p.name);
    profileData.put("mods", p.mods.toString());
    System.out.println(p.name + "..." + p.mods.toString());
    profileDataList.add(profileData);
    System.out.println("list.." + profileDataList.toString());
    profileListObj.put("profile"+i, profileDataList); // Is this really needed?   
}
//profileList.add(profileListObj);
obj.put("profiles", profileDataList);

Upvotes: 1

Related Questions