【发布时间】: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