我的sd卡中包含以下文本文件,其中包含json数组和对象,我很好地解析了第一个json数组“data”,现在我不知道如何解析第2和第3个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();
}
答案 0 :(得分:1)
你可以像对“数据”json数组那样做。
例如
JSONArray videos = jsonObj.getJSONArray("videos");
JSONArray images = jsonObj.getJSONArray("images");