java - 读取并附加到 json 文件

标签 java json parsing

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

相关文章:

jquery - 无法使用 jquery 显示希伯来语

xml - 在Gradle中,复制到项目后无法修改xml

java - 是否可以在运行时禁用android 6请求权限并使其从AndroidManifest正常工作?

java - 如何使用 JavaMail 进行 IMAP UID 搜索?

java - 在 Swing 中按下按钮之前阻止流程继续进行

javascript - Angular Schema 表单从 JSON 加载数据

java - 如何在 JAVA 中从 SOAP Web 服务请求、将 SOAP 响应转换为 XML 并将其与另一个 SOAP 响应进行比较

jquery - 对由 Json 数组填充的 JQuery 列表进行排序

c - C 中的可变参数 scanf

将 brainf*ck 代码解析为 Rust 中的树