我有一个json文件如下:
{
"profiles": {
"example1": {
"name": "example1",
"lastVersionId": "example1"
},
"example2": {
"name": "example2",
"lastVersionId": "example2",
"playerUUID": "00000000000000000000000000000000"
},
},
"selectedProfile": "example",
"clientToken": "000000000000000000000000000",
"authenticationDatabase": {
"000000000000000000000000000": {
"username": "example@gmail.com",
"accessToken": "000000000000000000000000000",
"userid": "000000000000000000000000000",
"uuid": "000000000000000000000000000",
"displayName": "example"
}
}
}
我需要按如下方式附加:
{
"profiles": {
"example1": {
"name": "example1",
"lastVersionId": "example1"
},
"example2": {
"name": "example2",
"lastVersionId": "example2",
"playerUUID": "00000000000000000000000000000000"
},
"example3": {
"name": "example3",
"lastVersionId": "example3",
},
},
"selectedProfile": "example",
"clientToken": "000000000000000000000000000",
"authenticationDatabase": {
"000000000000000000000000000": {
"username": "example@gmail.com",
"accessToken": "000000000000000000000000000",
"userid": "000000000000000000000000000",
"uuid": "000000000000000000000000000",
"displayName": "example"
}
}
}
所以基本上,我需要在文件中添加一个配置文件。
以下是当前不成功的代码,因为格式无法正确写入文件。
package minecraftMPC;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import org.json.simple.JSONObject;
import org.json.simple.parser.JSONParser;
import org.json.simple.parser.ParseException;
public class Profile {
public static void main(String[] args) throws FileNotFoundException, IOException, ParseException {
profileAppend("test", "exampleName");
}
@SuppressWarnings("unchecked")
public static void profileAppend(String profile, String version)
throws FileNotFoundException, IOException, ParseException {
JSONParser parser = new JSONParser();
Object obj = parser.parse(new FileReader(GetProperties.mcDir()
+ "/launcher_profiles.json"));
JSONObject jsonObject = (JSONObject) obj;
JSONObject profiles = (JSONObject) jsonObject.get("profiles");
String selectedProfile = profile;
String clientToken = (String) jsonObject.get("clientToken");
JSONObject authenticationDatabase = (JSONObject) jsonObject
.get("authenticationDatabase");
JSONObject params = new JSONObject();
params.put("lastVersionId", version);
params.put("name", profile);
profiles.put(profile, params);
JSONObject test = new JSONObject();
test.put("profiles", profiles);
test.put("selectedProfile", selectedProfile);
test.put("clientToken", clientToken);
test.put("authenticationDatabase", authenticationDatabase);
FileWriter file = new FileWriter(GetProperties.mcDir()
+ "/launcher_profiles-test.json");
file.write(test.toJSONString());
file.flush();
file.close();
System.out.println(test);
}
}
输出:
{"selectedProfile": "example", "profiles": {"example1": { "name": "example1","lastVersionId": "example1"}, "example2": {"name": "example2", "lastVersionId": "example2", "playerUUID": "00000000000000000000000000000000" }, "example3": {"name": "example3", "lastVersionId": "example3",}, },"clientToken": "000000000000000000000000000", "authenticationDatabase": { "000000000000000000000000000": { "username": "example@gmail.com", "accessToken": "000000000000000000000000000", "userid": "000000000000000000000000000", "uuid": "000000000000000000000000000", "displayName": "example" } } }
答案 0 :(得分:1)
您的输出符合预期,只是格式化为一行而不是多行。