【问题标题】:Reading and appending to a json file读取并附加到 json 文件
【发布时间】:2014-03-10 01:05:18
【问题描述】:

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


【解决方案1】:

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

【讨论】:

  • 那我怎么让它多行呢?
  • 也是顺序不对,selectedProfile输出第一个而不是第二个。
  • 我需要它写入上述示例中的文件,而不是一行。
  • 在实数 JSON 顺序中也无关紧要,因为字段由名称标识。格式化无关紧要,因为它通常是由代码而不是由人读取的。我在上面分享的链接只是为了方便查看它以更具可读性的形式看起来如何,但它在包含的数据方面没有任何区别。
  • 我明白,这是出于个人喜好和可读性。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2016-08-26
  • 2022-12-05
  • 1970-01-01
相关资源
最近更新 更多