我正在做JSON。我写的代码可以解析JSON并显示列表视图(图像和文本)。现在,我想将我的JSON保存在文件(json.txt)中。这是我的代码。我试图保存JSON,但当我在我的json.txt文件上调试它时,只保存了第一个数据,但我在JSON中有20个数据,如果有人知道解决方案,请帮助......
jsonparser = new JSONParser();
JSONObject jsonobject = jsonparser.getJSONfromURL(URL);
try {
jsonarray = jsonobject.getJSONArray("data");
for (int i = 0; i < jsonarray.length(); i++) {
jsonobject = jsonarray.getJSONObject(i);
HashMap<String, String> map = new HashMap<String, String>();
map.put("journal", jsonobject.getString(KEY_journal));
map.put("image", jsonobject.getString(KEY_image));
map.put("title", jsonobject.getString(KEY_title));
map.put("description",
jsonobject.getString(KEY_description));
map.put("JournalID", jsonobject.getString(KEY_JournalID));
map.put("pubDate", jsonobject.getString(KEY_pubDate));
map.put("statID", jsonobject.getString(KEY_statID));
Content cont = new Content(jsonobject.getString("journal"),
jsonobject.getString("image"),
jsonobject.getString("title"),
jsonobject.getString("pubDate"),
jsonobject.getString("description"),
jsonobject.getString("JournalID"),
jsonobject.getString("statID"));
contents.add(cont);
yourFile = new File("/sdcard/json.txt");
try {
writer = new OutputStreamWriter(
new FileOutputStream(yourFile), "UTF-8");
writer.write(jsonobject.toString());
writer.close();
} catch (IOException e) {
e.printStackTrace();
} finally {
if (writer != null) {
try {
writer.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}发布于 2014-01-30 19:32:47
在追加模式下打开文件。
new OutputStreamWriter(new FileOutputStream(yourFile,true), "UTF-8");发布于 2014-01-30 19:40:28
对从URL中检索到的JSONObject和用于循环数据数组的变量使用单独的变量:
jsonparser = new JSONParser();
JSONObject jsonfromurl = jsonparser.getJSONfromURL(URL);
try {
jsonarray = jsonfromurl.getJSONArray("data");
for (int i = 0; i < jsonarray.length(); i++) {
JSONObject jsonobject = jsonarray.getJSONObject(i);
HashMap<String, String> map = new HashMap<String, String>();
map.put("journal", jsonobject.getString(KEY_journal));
map.put("image", jsonobject.getString(KEY_image));
map.put("title", jsonobject.getString(KEY_title));
map.put("description",
jsonobject.getString(KEY_description));
map.put("JournalID", jsonobject.getString(KEY_JournalID));
map.put("pubDate", jsonobject.getString(KEY_pubDate));
map.put("statID", jsonobject.getString(KEY_statID));
Content cont = new Content(jsonobject.getString("journal"),
jsonobject.getString("image"),
jsonobject.getString("title"),
jsonobject.getString("pubDate"),
jsonobject.getString("description"),
jsonobject.getString("JournalID"),
jsonobject.getString("statID"));
contents.add(cont);
yourFile = new File("/sdcard/json.txt");
try {
writer = new OutputStreamWriter(
new FileOutputStream(yourFile), "UTF-8");
writer.write(jsonfromurl.toString());
writer.close();
} catch (IOException e) {
e.printStackTrace();
} finally {
if (writer != null) {
try {
writer.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}发布于 2014-01-30 19:46:57
您应该遵循与here相同的风格。据我所知,它们实际上将字节写入FileOutputStream,而您尝试写入字符串。根据documentation,FileOutputStream只接受字节。
请尝试使用writer.write(jsonobject.toString().getBytes());。
https://stackoverflow.com/questions/21455143
复制相似问题