读书人

json 掏出之

发布时间: 2013-04-07 12:50:11 作者: rapoo

json 取出之


{"contents":[{"productID":34,"productName":"SuperWidget"},{"productID":56,"productName":"WonderWidget"}]}


端拉取的料


private void saveFile() {
try {
FileOutputStream fileOutputStream = openFileOutput("123.txt",
Context.MODE_PRIVATE);
fileOutputStream.write(resource.getBytes());
fileOutputStream.flush();
fileOutputStream.close();
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
Toast.makeText(JSONExampleActivity.this, "存成功", Toast.LENGTH_LONG).show();
}

存在Internal Storage

之後取

private void readFile() {
String temp = null;
try {
FileInputStream fileInputStream = openFileInput("123.txt");
int length = fileInputStream.available();
byte[] buffer = new byte[length];
fileInputStream.read(buffer);
fileInputStream.close();
temp = new String(buffer);
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}


}


readFile()的temp 要如何才被段行呢?
JSONArray test = temp.getJSONArray("contents");

我知道宣告

我也把它成JSONObject型 但是都是 高人指 已爬多文


[解决办法]
JSONArray array = new JSONArray(temp);

for(int i=0;i<array.size();i++){
JSONObject obj = array.get(i);
String str = obj.getString("productID");
}

读书人网 >Android

热点推荐