簡體   English   中英

讀取並附加到json文件

[英]Reading and appending to a json file

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

您的輸出符合預期,只是格式化為一行而不是多行。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM