簡體   English   中英

如何從SD卡讀取json文件

[英]How can I read json file from SD Card

我需要從SD卡讀取一個json文件並在微調器中顯示數據。 有沒有辦法從Android中的文件讀取數據並在微調器中顯示該文件的內容?

首先從SD卡讀取文件,然后解析該文件

步驟1從SD卡中檢索文件中的數據。 教程

步驟2解析數據。 請參見如何解析JSON字符串

示例代碼

try {

            File dir = Environment.getExternalStorageDirectory();
            File yourFile = new File(dir, "path/to/the/file/inside/the/sdcard.ext");
            FileInputStream stream = new FileInputStream(yourFile);
            String jString = null;
            try {
                FileChannel fc = stream.getChannel();
                MappedByteBuffer bb = fc.map(FileChannel.MapMode.READ_ONLY, 0, fc.size());
                /* Instead of using default, pass in a decoder. */
                jString = Charset.defaultCharset().decode(bb).toString();
              }
              finally {
                stream.close();
              }


                    JSONObject jObject = new JSONObject(jString); 



        } catch (Exception e) {e.printStackTrace();}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM