我从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);
或者,您也可以根据需要添加项目。
(很抱歉写错了,我在智能手机上)