这个问题已经有了答案:
Sending and Parsing JSON Objects in Android [closed]
11个答案
我的SDCard中有以下文本文件。现在我想通过JSON解析器解析这个文件,我想解析并读取这个文件,并将数据显示到我的文本视图中,我该怎么做?

{
"data": [
    {
        "id": "1",
        "title": "Farhan Shah",
        "duration": 10,
    },
    {
        "id": "2",
        "title": "Noman Shah",
        "duration": 10,
    },
    {
        "id": "3",
        "title": "Ahmad Shah",
        "duration": 10,
    },
    {
        "id": "4",
        "title": "Mohsin Shah",
        "duration": 10,
    },
    {
        "id": "5",
        "title": "Haris Shah",
        "duration": 10,
    }
  ]

}

这是代码:
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();

我得到了文件,但现在我不知道如何解析数据?

最佳答案

从SD卡中使用File读取JSON文件,并使用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, 0, 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        \"duration\": 10\n    },\n    {\n        \"id\": \"3\",\n        \"title\": \"Ahmad Shah\",\n        \"duration\": 10\n    },\n    {\n        \"id\": \"4\",\n        \"title\": \"Mohsin Shah\",\n        \"duration\": 10\n    },\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" />

10-07 19:35
查看更多