-1

我被卡住了,我在 SD 卡中有一个 json 文本文件,我需要从 SD 卡中读取一个 json 文件并将数据显示到我的 textview 中。

这是我的 json 文本文件名是 textarabics.json:

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

}

我想在我的文本视图中显示“标题”,“持续时间”是秒数,例如当第一个“标题”显示到我的文本视图中时,它会在屏幕上显示 10 秒,之后第二个文本显示 x 秒等。在我的 xml 文件中,我只有一个 textview。任何想法和帮助将不胜感激。提前感谢您的宝贵答复。

4

1 回答 1

1

您可以这样做:
1-读取文件
2- 获取文本并解析它:

JSONObject json = new JSONObject(text);
JSONArray ar = new JSONArray(json.getString("data"));
for (int i = 0; i < ar.length(); i++) {
     try {
          JSONObject jpost = ar.getJSONObject(i);
          // Pulling items from the array
          int id = jpost.getInteger("id");
          String title = jpost.getString("title");
          int duration = jpost.getInteger("duration");
          // DO whatever
          //Ex.
          //textView.setText(title);
          //if you use "Thread", you can put Thread.sleep(duration*1000);
     } catch (JSONException e) {
          // Oops
          e.printStackTrace();
     }
}

希望这会有所帮助
注意:cause there is comma after duration您的 json 文件中可能会出现错误

于 2014-02-23T08:24:31.217 回答