JavaJSON写jsonarray循环

JavaJSON写jsonarray循环,java,arrays,json,Java,Arrays,Json,我正在尝试使用以下代码写入json文件: JSONObject obj = new JSONObject(); JSONArray dlist = new JSONArray(); JSONObject data = new JSONObject(); //Data data.put("path", gamePath); data.put("lastprofile", profileList.getSelectedValue()); dli

我正在尝试使用以下代码写入json文件:

    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);
问题是什么,你可以看到我修改过的一些注释行。整个阵列最终将保存为最后一个对象配置文件

我想要的是:

profiles 
-profile1
--data
--data
-profile2
--data
--data etc, 
下面是它使用上面的示例代码生成的内容

{
    "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]"
        }
    ]
}
我已经修改了多个注释掉的行,尽量使所有数据都正确,只是没有在概要文件中进行组织。我正试图让这是尽可能干净,所以它是有组织的

我得出的结论是,我无法将具有相同密钥的内容放入profileData中。因此,它将第一个输入重新添加到profileDataList中,并在之后继续执行每个循环


更多信息:每个profileData名称和mod都有不同的字符串。有5种不同的配置文件。配置文件应在配置文件内部命名为配置文件,并在其后面加上相应的编号。

当您执行
profileData.put时(“name”,p.name)您正在覆盖同一个对象,因此在最后一步中,您将拥有一个数组,其中包含对同一对象的3个引用。要修复此问题,请在循环内创建一个新实例(请参见注释):

for(int i=profiles.size()-1;i>=0;i--){
Profile p=profiles.get(i);
profileData=new JsonObject();//谢谢你的回答:)我已经测试过了,这确实是解决方案!
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);