-1

I am stuck,i have a json text file in Sd Card and i need to read a json file from the SD card and display the data in to my textview.

This is my json textfile name is 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,
    }
 ]

}

I want to display the "title" into my textview,and the "duration" is the number of secondes,for example when 1st "title" display into my textview,then it will be visibl on the screen for 10seconds,after that second text display for x seconds and so on.in my xml file i have a just one textview.any idear and help will be much much appreciated..Thanks for ur Valuable Replies in advance.

Farhan Shah
  • 2,344
  • 7
  • 28
  • 54

1 Answers1

1

You can do it like this:
1- Read file
2- Take the text and parse it :

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();
     }
}

hope this helps
Note: You may get Error cause there is comma after duration in your json file

Community
  • 1
  • 1
Saif Hamed
  • 1,084
  • 1
  • 12
  • 17