首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >读取并附加到json文件

问读取并附加到json文件
EN

Stack Overflow用户
提问于 2014-03-10 09:05:19
回答 1查看 3.2K关注 0票数 2

我有一个json文件,如下所示:

代码语言:javascript
复制
{
  "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"
    }
  }
}

我需要添加如下内容:

代码语言:javascript
复制
{
  "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"
    }
  }
}

所以基本上,我需要在文件中添加一个配置文件。

这是当前未成功的代码,因为格式不能正确地写入文件。

代码语言:javascript
复制
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);
    }
}

输出:

代码语言:javascript
复制
{"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" } } }
EN

回答 1

Stack Overflow用户

发布于 2014-03-10 09:15:28

您的输出与预期不谋而合,只是格式化成一行而不是多行。

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/22290457

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档