Android   发布时间:2022-04-28  发布网站:大佬教程  code.js-code.com
大佬教程收集整理的这篇文章主要介绍了Android如何从SD卡读取json文件(文本文件),并将数据显示到textview大佬教程大佬觉得挺不错的,现在分享给大家,也给大家做个参考。
这个问题在这里已经有一个答案:> Sending and Parsing JSON Objects11 @H_696_2@我有以下文本@L_197_4@到我的SdCard.Now我想通过Json解析器解析这个@L_197_4@,我想解析并读取这个@L_197_4@并将数据显示在我的文本视图中,我该怎么做?
{
"data": [
    {
        "id": "1","title": "Farhan Shah","duration": 10,},{
        "id": "2","title": "Noman Shah",{
        "id": "3","title": "Ahmad Shah",{
        "id": "4","title": "Mohsin Shah",{
        "id": "5","title": "Haris Shah",}
  ]

}

这是代码

File sdcard = Environment.getExternalStorageDirectory();

    //Get the text file

    File file = new File(sdcard,"textarabics.txt");

    String UTF8 = "utf8";
    int BUFFER_SIZE = 8192;


    //Read text from file
    StringBuilder text = new StringBuilder();

我得到的@L_197_4@,但现在我不知道我如何解析数据?

@H_874_11@

解决方法

使用 File从SD卡读取JSON@L_197_4@,并使用 JSONObject进行解析.

这里有一个起始码:

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

public class ReadJsonFile {
public ReadFile (){ 
            try {
            File yourFile = new File(Environment.getExternalStorageDirectory(),"path/to/the/file/inside_the_sdcard/textarabics.txt");
            FileInputStream stream = new FileInputStream(yourFilE);
            String jsonStr = null;
            try {
                FileChAnnel fc = stream.getChAnnel();
                MappedByteBuffer bb = fc.map(FileChAnnel.MapMode.READ_ONLY,fc.size());

                jsonStr = Charset.defaultCharset().decode(bb).toString();
              }
              catch(Exception E){
              e.printStackTrace();
              }
              finally {
                stream.close();
              }
/*  String jsonStr = "{\n\"data\": [\n    {\n        \"id\": \"1\",\n        \"title\": \"Farhan Shah\",\n        \"duration\": 10\n    },\n    {\n        \"id\": \"2\",\n        \"title\": \"Noman Shah\",\n    {\n        \"id\": \"3\",\n        \"title\": \"Ahmad Shah\",\n    {\n        \"id\": \"4\",\n        \"title\": \"Mohsin Shah\",\n    {\n        \"id\": \"5\",\n        \"title\": \"Haris Shah\",\n        \"duration\": 10\n    }\n  ]\n\n}\n";
  */     
                 JSONObject jsonObj = new JSONObject(jsonStr);

                // GetTing data JSON Array nodes
                JSONArray data  = jsonObj.getJSONArray("data");

                // looping through All nodes
                for (int i = 0; i < data.length(); i++) {
                    JSONObject c = data.getJSONObject(i);

                    String id = c.getString("id");
                    String title = c.getString("title");
                    String duration = c.getString("duration");
                    //use >  int id = c.geTint("duration"); if you want get an int


                    // tmp hashmap for single node
                    HashMap<String,String> parsedData = new HashMap<String,String>();

                    // adding each child node to HashMap key => value
                    parsedData.put("id",id);
                    parsedData.put("title",titlE);
                    parsedData.put("duration",duration);


                    // do what do you want on your interface
                  }


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

不要忘记在你的主播上添加读取存储:

<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
@H_874_11@ @H_874_11@

大佬总结

以上是大佬教程为你收集整理的Android如何从SD卡读取json文件(文本文件),并将数据显示到textview全部内容,希望文章能够帮你解决Android如何从SD卡读取json文件(文本文件),并将数据显示到textview所遇到的程序开发问题。

如果觉得大佬教程网站内容还不错,欢迎将大佬教程推荐给程序员好友。

本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。