1
我從JSONArray.put(String)
得到意想不到的結果。使用來自緩衝讀取器的JSONArray.put(字符串)的意外結果
我的代碼:
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\" .... "]"]
這是怎麼發生的?
向我們顯示文件內容的簡短版本。 –
替換'readedID.put(line);''用'readedID = new JSONArray(line);' – Madhan
Madhan謝謝你! –