0

I have the following textfile into my sd card which contains json arrays and objects,i am suucessfully parse the 1st json array "data",now i have no idea how i can parse 2nd and 3rd json arrays "videos" and "images"?all the data e.g videos and images are placed in a sd card.any help will be much appreciated..Thanks

my textfile "textarabics.txt" which contains jsona arrays and objects:

{
"data": [
    {
        "id": "1",
        "title": "تخطي نوجا نوجا أخبار واستعراض السوق",
        "duration": 10
    },
    {
        "id": "2",
        "title": "أحدث نوجا الأخبار وآخر المستجدات",
        "duration": 3
    },
    {
        "id": "3",
        "title": "نوجا الأخبار وآخر المستجدات",
        "duration": 5
    },
    {
        "id": "4",
        "title": "لا تحتوي على تطبيقات وجد نوع الكلمة",
        "duration": 7
    },
    {
        "id": "5",
        "title": "تحتاج إلى إعادة تشغيل التطبيق لاتخاذ تغييرات الخط. هل تريد إعادة التشغيل الآن",
        "duration": 4
    }
],
"videos": [

    {
        "id": "1",
        "video": "VEVUE_video-2013-11-21-16-50-20.mp4"

    },
    {
        "id": "2",
        "video": "VEVUE_video-2013-12-30-17-47-44.mp4"

    },
    {
        "id": "3",
        "video": "video-2013-09-18-12-41-59.mp4"

    }

  ],
  "images": [
    {
        "bgimage": "nogastorebgimage.png",
        "logoimage": "welcometonoga.png"

    }

  ]
  }

my code which i have successfully parse 1st json array "data" and objects:

try {
        File yourFile = new File(Environment.getExternalStorageDirectory(), "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.forName("UTF-8").decode(bb).toString();
            Log.d("Noga Store", "jString = " + jsonStr);
          }
          finally {
            stream.close();
          }

             Log.d("Noga Store", "jString = " + jsonStr);
                     // A JSONTokener is needed in order to use JSONObject correctly
             JSONTokener jsonTokener = new JSONTokener(jsonStr);
                     // Pass a JSONTokener to the JSONObject constructor
             JSONObject jsonObj = new JSONObject(jsonTokener);


            // 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");
                int duration = c.getInt("duration");

                // 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);*/

                textnames.add(title);
                textduration.add(duration);
            //    textnames.add(duration);
                // do what do you want on your interface
              }


       } catch (Exception e) {
       e.printStackTrace();
      }
Farhan Shah
  • 2,344
  • 7
  • 28
  • 54

1 Answers1

1

You can do same as you did for "data" json array

for example

JSONArray videos = jsonObj.getJSONArray("videos");
JSONArray images = jsonObj.getJSONArray("images");
Sonali8890
  • 2,005
  • 12
  • 16
  • ok,and my videos file are placed in sd card and in a folder name "nogastore"..so can i get videos if i doing same process from video? – Farhan Shah Feb 25 '14 at 07:03
  • you have to do extra efforts for that. json response will return only file name not full path of video, so you have to add file path before accessing that file – Sonali8890 Feb 25 '14 at 07:06
  • you have to do something like "/sdcard/nogastore/"+your_filename – Sonali8890 Feb 25 '14 at 07:08