gpt4 book ai didi

java - 读取并附加到 json 文件

转载 作者:行者123 更新时间:2023-11-30 11:21:19 26 4
gpt4 key购买 nike

我有一个 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" } } }

最佳答案

您的输出符合预期,只是格式化为一行而不是多行。

关于java - 读取并附加到 json 文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22290457/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com