-1

私は立ち往生しています.SDカードにJSONテキストファイルがあり、SDカードからJSONファイルを読み取り、テキストビューにデータを表示する必要があります。

これは私の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,
    }
 ]

}

「タイトル」をテキストビューに表示したいのですが、「期間」は秒数です。たとえば、最初の「タイトル」がテキストビューに表示されると、その2番目のテキストの後、画面に10秒間表示されますx 秒間表示するなどです。私の xml ファイルには、テキストビューが 1 つしかありません。

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 に答える