2014-02-25 58 views
0

我有以下文本文件到我的SD卡,其中包含JSON數組和對象,我suucessfully解析第一個JSON數組「數據」,現在我不知道我是如何可以解析第二個和第三個json數組「視頻」和「圖像」?所有的數據,例如視頻和圖像被放置在sd卡中。任何幫助將不勝感激..謝謝Android如何從SD卡解析多個JSON數組和對象

my textfile「textarabics.txt」which包含jsona數組和對象:

{ 
"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" 

    } 

    ] 
    } 

我的代碼,我已成功解析第一JSON數組「數據」和對象:

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

回答

1

像你一樣的「數據」 JSON數組

例如

JSONArray videos = jsonObj.getJSONArray("videos"); 
JSONArray images = jsonObj.getJSONArray("images"); 
+0

ok了,我的視頻文件被放置在SD卡和文件夾名稱「nogastore」你可以做同樣的..我可以得到視頻,如果我從視頻做相同的過程? –

+0

你必須爲此做額外的努力。 json響應將只返回文件名不完整的視頻路徑,所以你必須在訪問該文件之前添加文件路徑 – Sonali8890

+0

你必須做一些像「/ sdcard/nogastore /」+ your_filename – Sonali8890