繁体   English   中英

Android如何从SD卡解析多个JSON数组和对象

[英]Android how to parse multiple json arrays and objects from sd card

我的SD卡中包含以下文本文件,其中包含json数组和对象,我成功解析了第一个json数组“ data”,现在我不知道如何解析第二个和第三个json数组“视频”和“图像”?所有数据(例如视频和图像)都存储在SD卡中。任何帮助将不胜感激。

我的文本文件“ textarabics.txt”包含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();
      }

您可以执行与“数据” json数组相同的操作

例如

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM