我是Android新手。我有一个包含多个数组的JSON文件,并且能够从第一个数组中获取数据并将其显示在listview中(请检查第一个for循环)。但是我试图获取下一个数组,但没有得到任何结果(黑屏)。我需要显示“ open_days”数组中的值。发布了我的JSON文件和循环部分。请检查并帮助我
MainActivity。 Java
try {
JSONObject jjsonObject =new JSONObject(result);
String getObject = jjsonObject.getString("message");
String getObject1 = jjsonObject.getString("status");
JSONArray getArray = jjsonObject.getJSONArray("favorite_facility_list");
Log.e("message",getObject);
Log.e("status",getObject1);
Log.e("favorite_facility_list",getArray.toString());
for(int i = 0; i < getArray.length(); i++)
{
JSONObject objects = getArray.getJSONObject(i);
String promotion_id = objects.getString("facility_id");
String promotion_image_name = objects.getString("facility_course_name");
String promotion_image_url = objects.getString("icon_image_name");
JSONArray array2=jjsonObject.getJSONArray("open_days");
for(int j=0;j<array2.length();j++)
{
JSONObject obj2=array2.getJSONObject(j);
String openid = obj2.getString("open_days_id");
String openname = obj2.getString("open_days_name");
//
promotionlists promotionlists = new promotionlists();
promotionlists.setFacility_id(promotion_id);
promotionlists.setFacility_course_name(promotion_image_name);
promotionlists.setIcon_image_name(promotion_image_url);
promotionlists.setOpen_days_id(openid);
promotionlists.setOpen_days_name(openname);
promotionArray.add(promotionlists);
adapter= new CustomAdapter(promotionArray,getApplicationContext());
lv.setAdapter(adapter);
}}
JSON文件:
{
"status": "1",
"message": "Success",
"favorite_facility_list": [
{
"facility_id": "11",
"facility_course_name": "Facility 2",
"icon_image_name": "3.jpg",
"banner_image_name_list": [
"39.jpg"
],
"address": "test1",
"open_days": [
{
"open_days_id": "5",
"open_days_name": "Thursday"
},
{
"open_days_id": "6",
"open_days_name": "Friday"
},
{
"open_days_id": "8",
"open_days_name": "Sunday"
},
{
"open_days_id": "7",
"open_days_name": "Saturday"
}
],
"open_time_start": "6:8 pm",
"open_time_end": "12:8 pm",
"workouts": [
{
"workout_id": "1",
"workout_name": "Aerobic"
},
{
"workout_id": "2",
"workout_name": "Yoga"
},
{
"workout_id": "4",
"workout_name": "Gym"
},
{
"workout_id": "8",
"workout_name": "Pool"
}
],
"equipments": [
{
"equipment_id": "5",
"equipment_name": "Rowing"
},
{
"equipment_id": "6",
"equipment_name": "Stationary Bike"
},
{
"equipment_id": "7",
"equipment_name": "Bench Press"
},
{
"equipment_id": "8",
"equipment_name": "TRX"
}
],
"services": [
{
"service_id": "4",
"service_name": "Tranning"
},
{
"service_id": "1",
"service_name": "Personal Training"
}
],
"drop_in_rate": [
"10-test",
"20-test1",
"30-test3"
],
"parking_status": "1",
"parking_notes": "2 hrs available",
"facility_status": "1",
"facility_status_description": "Live",
"rating": "2.90",
"distance": "9412.2ml",
"already_like": 1,
"weburl": "",
"phone_no": "",
"facility_lattitude": "-3",
"facility_longitude": "151"
}
]
}
答案 0 :(得分:2)
您的Logcat抛出JSONException
。
不要
JSONArray array2=jjsonObject.getJSONArray("open_days");
要做
JSONArray array2=objects.getJSONArray("open_days");
答案 1 :(得分:2)
您使用了错误的对象来提取open_days
更改
JSONArray array2 = jjsonObject.getJSONArray("open_days");
到
JSONArray array2 = objects.getJSONArray("open_days");