Java 读取并附加到json文件
我有一个json文件,如下所示:Java 读取并附加到json文件,java,json,parsing,Java,Json,Parsing,我有一个json文件,如下所示: { "profiles": { "example1": { "name": "example1", "lastVersionId": "example1" }, "example2": { "name": "example2", "lastVersionId": "example2", "playerUUID": "000000000000000000000000000000
{
"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"
}
}
}
我需要附加如下内容:
{
"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);
}
}
以下是当前不成功的代码,因为格式无法正确写入文件
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" } } }
您的输出与预期一样,只是格式化为一行而不是多行。您提供的输出是预期的还是实际的?您实际完成了输出,并且与预期相同,只是在一行中输出。:)那么,如何将其设置为多行呢?它的顺序也是错误的,selectedProfile输出的是第一行,而不是第二行。在上面的示例中,我需要它写入文件,而不是一行。同样,JSON顺序也不重要,因为字段是通过名称标识的。格式化并不重要,因为它通常由代码读取,而不是由人读取。我在上面分享的链接只是为了方便查看它在更可读的形式中的外观,但它在包含的数据方面没有任何区别。我理解,这是为了个人喜好和可读性。