如何在java中将两个json文件合并在一起

时间:2011-08-10 07:44:50

标签: java json merge concat

我已经完成了我的应用程序,当它完成时会有两个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:java.lang.String类型的值+k V * can无法转换为JSONObject。谁知道发生了什么?

1 个答案:

答案 0 :(得分:0)

for (File file : list) {
  jsonList.put(new JSONObject(readFile(file));
}

String readFile(File file) {
....
}
相关问题