【问题标题】:How to merge two json files together in java如何在java中将两个json文件合并在一起
【发布时间】:2011-10-23 20:37:25
【问题描述】:

我已经完成了我的应用程序,当它完成时会有两个 json 文件。我需要将它们组合在一个不同的 java 类中,所以我尝试了类似的方法

如果我尝试使用此代码

File dirSrc = new File(mydir);


        File[] list = dirSrc.listFiles();
        JSONArray jsonList = new JSONArray();   
        for (File file : list) {
              try {
                jsonList.put(new JSONObject(readFile(file)));
            } catch (JSONException e) {



                e.printStackTrace();
            }
            }

        System.out.println(jsonList);




                private String readFile(File file) {
                String finalOutput = null;
                 try { 
        BufferedReader in = new BufferedReader(new FileReader(file));
        String str;
        while ((str = in.readLine()) != null) {
            finalOutput = str;
        }
        in.close();
    } catch (IOException e) {
    }
    return finalOutput;

}

我得到一个 org.json.JSONException: Value +k�V��䱐*ʜ� type java.lang.String 无法转换为 JSONObject。有谁知道怎么回事?

【问题讨论】:

  • 这些要如何合并?串联?结合其中一个有优先权?
  • 是的,我希望将它们连接起来 - 第一个与第二个连接
  • 请阅读以下答案:stackoverflow.com/a/9912406

标签: java json merge concat


【解决方案1】:
for (File file : list) {
  jsonList.put(new JSONObject(readFile(file));
}

String readFile(File file) {
....
}

【讨论】:

  • 它说需要将json解析器创建为一个类?我已经导入了 import org.json.*;
  • 您使用的库与我想象的不同。更新了代码。
猜你喜欢
  • 1970-01-01
  • 2011-04-04
  • 1970-01-01
  • 1970-01-01
  • 2015-12-27
  • 2017-12-21
  • 2021-11-30
  • 2016-08-23
相关资源
最近更新 更多