开发者

How to merge two json files together in java

开发者 https://www.devze.com 2023-03-27 03:54 出处:网络
I\'ve finished my application, and when it finishes there will be two json files. I need to combine them in a different java class so i\'ve tried something like this

I've finished my application, and when it finishes there will be two json files. I need to combine them in a different java class so i've tried something like this

If i try开发者_开发技巧 to use this code

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;

}

i get a org.json.JSONException: Value +k�V��䱐*ʜ� of type java.lang.String cannot be converted to JSONObject. Anyone knows what's up?


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

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

精彩评论

暂无评论...
验证码 换一张
取 消