我从 JSONArray.put(String)
得到了意外结果。
我的代码:
public JSONArray readedID = new JSONArray();
public void load() throws IOException, JSONException {
String FILENAME = "news";
String line;
InputStream inputStream = openFileInput(FILENAME);
InputStreamReader isr = new InputStreamReader(inputStream);
BufferedReader reader = new BufferedReader(isr);
while ((line = reader.readLine()) != null) {
readedID.put(line);
}
inputStream.close();
Log.d("vk", "news.readedID: "+readedID);
TextView res = (TextView)findViewById(R.id.textView);
res.setText("readedID: "+readedID);
}
public void save() throws IOException {
String FILENAME = "news";
FileOutputStream fos = openFileOutput(FILENAME, Context.MODE_PRIVATE);
fos.write(news.loadedID.toString().getBytes());
fos.close();
}
// Fill loadedID
for (int i = 0; i < json.length(); i++){
loadedID.put(json.getJSONObject(i).get("id").toString());
}
这是loadedID
:
["3316","3336","3335","3331","3327","3326","3319".... ]
这就是我对 readedID
的期望:
["3316","3336","3335","3331","3327","3326","3319" .... ]
但这就是我得到的:
["\"3316\",\"3336\",\"3335\",\"3331\",\"3327\",\"3326\",\"3319\" .... "]"]
为什么会发生这种情况?
最佳答案
嗯...这个\是因为您将列表项创建为字符串。
现在每个“都变成了\”,因此它是一个字符。
只需用新的 JSONArray 替换旧的 JSONArray:
readedID = new JSONArray(line);
或者您可以根据需要添加一个项目。
(抱歉,我在智能手机上写错了)
关于java - 使用 BufferedReader 中的 JSONArray.put(String) 出现意外结果,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30967766/