Android如何从sd卡解析多个json数组和对象
我的sd卡中有以下文本文件,其中包含json数组和对象,我正在成功解析第一个json数组“数据”,现在我不知道如何解析第二个和第三个json数组“视频”和“图像”?所有数据(例如视频和图像)都放在sd卡中。非常感谢您的帮助..谢谢 我的文本文件“textarabics.txt”,其中包含jsona数组和对象:Android如何从sd卡解析多个json数组和对象,android,json,android-json,Android,Json,Android Json,我的sd卡中有以下文本文件,其中包含json数组和对象,我正在成功解析第一个json数组“数据”,现在我不知道如何解析第二个和第三个json数组“视频”和“图像”?所有数据(例如视频和图像)都放在sd卡中。非常感谢您的帮助..谢谢 我的文本文件“textarabics.txt”,其中包含jsona数组和对象: { "data": [ { "id": "1", "title": "تخطي نوجا نوجا أخبار واستعراض السوق",
{
"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"
}
]
}
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数组“数据”和对象的代码:
{
"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"
}
]
}
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();
}
试试看{
File yourFile=新文件(Environment.getExternalStorageDirectory(),“textarabics.txt”);
FileInputStream=新的FileInputStream(您的文件);
字符串jsonStr=null;
试一试{
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存储”、“jString=“+jsonStr”);
}
最后{
stream.close();
}
Log.d(“Noga存储”、“jString=“+jsonStr”);
//为了正确使用JSONObject,需要一个JSONTokener
JSONTokener JSONTokener=新的JSONTokener(jsonStr);
//将JSONTokener传递给JSONObject构造函数
JSONObject jsonObj=新的JSONObject(jsonTokener);
//获取JSON数组节点的数据
JSONArray data=jsonObj.getJSONArray(“数据”);
//循环通过所有节点
对于(int i=0;ivalue
parsedData.put(“id”,id);
parsedData.put(“title”,title);
parsedData.put(“持续时间”,持续时间)*/
textnames.add(title);
textduration.add(持续时间);
//textnames.add(持续时间);
//在你的界面上做什么
}
}捕获(例外e){
e、 printStackTrace();
}
您可以对“数据”json数组执行相同的操作
比如说
JSONArray videos = jsonObj.getJSONArray("videos");
JSONArray images = jsonObj.getJSONArray("images");
好的,我的视频文件被放在sd卡和一个名为“Nogastre”的文件夹中。所以,如果我从视频中执行相同的过程,我可以得到视频吗?你必须为此付出额外的努力。json响应将只返回文件名,而不是视频的完整路径,因此在访问该文件之前必须添加文件路径。您必须执行“/sdcard/nogastre/”+您的文件名之类的操作