首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 移动开发 > Android >

json 掏出之問題

2013-04-07 
json 取出之問題{contents:[{productID:34,productName:SuperWidget},{productID:56,productNa

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");
}

热点排行
Bad Request.