2017-06-03 123 views
-4

如何解析JSON響應 的JSON迴應如下如何解析以下JSON響應

{ "status": "SUCCESS", "error_code": "", "message": "", "trips": [ { "trip_id": 405, "arrival_time": "26-12-2016 2:01 pm", "departure_time": "25-12-2016 9:15 am", 

"available_seats": 36, "boarding_points": [ { "bp_address": "SARAI KALE KHAN", "bp_id": "1360160", "bp_name": "SARAI KALE KHAN", "bp_contact_number": "", "bp_landmark": 

"SARAI KALE KHAN", "bp_location": "SARAI KALE KHAN", "bp_prime": "false", "bp_time": "25-12-2016 9:15 am" 
}], "dropping_points": [ { "dp_address": "AGRA", "dp_id": "1360186", "dp_name": "AGRA CANTT RAILWAY STATION", "dp_contact_number": "", "dp_landmark": "AGRA", 

"dp_location": "AGRA CANTT RAILWAY STATION", "dp_prime": "false", "dp_time": "26-12-2016 2:01 pm" } ], "cancellation_policy": [ { "from_time": "25-12-2016 8:15 am", 

"to_time": "25-12-2016 9:15 am", "service_charge": "96.25", "seat_fare": 385 }, { "from_time": "24-12-2016 9:15 am", "to_time": "25-12-2016 8:15 am", "service_charge": 

"57.75", "seat_fare": 385 }, { "from_time": 0, "to_time": "24-12-2016 9:15 am", "service_charge": "0.00", "seat_fare": 385 } ], "bus_type": "JAN RATH A/C(2+2)", 

"fare_details": [ { "base_fare": "385.00", "other_tax": "0.00", "self_markup": "0.00", "total_fare": "385.00", "commission": 6.4295 
}], "travels_name": "Uttar Pradesh State Road Transport Corporation(UPSRTC)", "operator_id": "11044", "ac": "true", "seater": "true", "sleeper": "false", 

"vehicle_type": "BUS", "partial_cancellation_allowed": "false" } ] } 

感謝

+3

你嘗試過什麼到目前爲止? –

+0

你可以使用JSON.parse –

+0

是的,它的完成,謝謝你 – Ghanshyam

回答

0
JSONObject jsonObj = new JSONObject(Jstring); 
String status=jsonObj.getString("status"); 
String error_code=jsonObj.getString("error_code"); 
String message=jsonObj.getString("message"); 
JSONArray trips= jsonObj.getJSONArray("trips"); 
for (int i = 0; i < trips.length(); i++) { 
JSONObject trips_data = trips.getJSONObject(i); 
String trip_id=trips_data.getString("trip_id"); 
String arrival_time=trips_data.getString("arrival_time"); 
String departure_time=trips_data.getString("departure_time"); 
String available_seats=trips_data.getString("available_seats"); 

JSONArray boarding_points=trips_data.getJSONArray("boarding_points"); 
for (int j = 0; j < available_seats.length(); ++) { 
JSONObject boarding_points_data = boarding_points.getJSONObject(i); 
String bp_address=boarding_points_data.getString("bp_address"); 
String bp_id=boarding_points_data.getString("bp_id"); 
String bp_name=boarding_points_data.getString("bp_name"); 
String bp_contact_number=boarding_points_data.getString("bp_contact_number"); 
String bp_landmark=boarding_points_data.getString("bp_landmark"); 
String bp_location=boarding_points_data.getString("bp_location"); 
String bp_prime=boarding_points_data.getString("bp_prime"); 
String bp_time=boarding_points_data.getString("bp_time"); 
} 

JSONArray dropping_points=trips_data.getJSONArray("dropping_points"); 
for (int j = 0; j < dropping_points.length(); ++) { 
JSONObject dropping_points_data = dropping_points.getJSONObject(i); 
String dp_address=dropping_points_data.getString("dp_address"); 
String dp_id=dropping_points_data.getString("dp_id"); 
String dp_name=dropping_points_data.getString("dp_name"); 
String dp_contact_number=dropping_points_data.getString("dp_contact_number"); 
String dp_landmark=dropping_points_data.getString("dp_landmark"); 
String dp_location=dropping_points_data.getString("dp_location"); 
String dp_prime=dropping_points_data.getString("dp_prime"); 
String dp_time=dropping_points_data.getString("dp_time"); 
} 

JSONArray cancellation_policy=trips_data.getJSONArray("cancellation_policy"); 
for (int j = 0; j < cancellation_policy.length(); ++) { 
JSONObject cancellation_policy_data = cancellation_policy.getJSONObject(i); 
String from_time=cancellation_policy_data.getString("from_time"); 
String to_time=cancellation_policy_data.getString("to_time"); 
String service_charge=cancellation_policy_data.getString("service_charge"); 
String seat_fare=cancellation_policy_data.getString("seat_fare"); 

} 


JSONArray fare_details=trips_data.getJSONArray("fare_details"); 
for (int j = 0; j < fare_details.length(); ++) { 
JSONObject fare_details_data = fare_details.getJSONObject(i); 
String base_fare=fare_details_data.getString("base_fare"); 
String other_tax=fare_details_data.getString("other_tax"); 
String self_markup=fare_details_data.getString("self_markup"); 
String total_fare=fare_details_data.getString("total_fare"); 
String commission=fare_details_data.getString("commission"); 
} 

String bus_type=trips_data.getString("bus_type"); 
String travels_name=trips_data.getString("travels_name"); 
String operator_id=trips_data.getString("operator_id"); 
String ac=trips_data.getString("ac"); 
String seater=trips_data.getString("seater"); 
String sleeper=trips_data.getString("sleeper"); 
String vehicle_type=trips_data.getString("vehicle_type"); 
String partial_cancellation_allowed=trips_data.getString("partial_cancellation_allowed"); 
}