你好我是android.please全新的建议我如何解析下面的jsonresposne并得到值。任何帮助请建议我。我不知道如何解析,因为它包含方括号和大括号
`[
{
"bus_schedule":[
{
"bus_route":[
{
"serviceDate":"2016-12-31",
"amenities":" VB King Size Semi Sleeper (2 plus 1) selected WIFI,Water,Bottle,Blankets,Snacks,Movie,Food,Emergency exit, Fire Extinguisher,Bus Hostess,CCTV,Emergency Contact Number",
"startCityName":"Coimbatore",
"departureTime":"21:00:00",
"fare":"499",
"endCityName":"Kanyakumari",
"arrivalTime":"06:00:00",
"operatorName":"RUN TO WIN",
"bus_id":"17",
"journeyHours":"9",
"available_seat":25,
"total_seats":34,
"seat_type":"semi sleeper"
}
],"boardingPoint":[
{
"boardingPointName":"Thudiyalur",
"boardingPointContact":"8883088820",
"boardingPointTime":"21:25:00",
"boardingPointId":"316",
"BusId":"17"
},`
my code is
`protected Void doInBackground(Void... params) {
HttpGet request = new HttpGet(url);
ResponseHandler<String> responseHandler = new BasicResponseHandler();
try {
response= mClient.execute(request, responseHandler);
} catch (ClientProtocolException exception) {
exception.printStackTrace();
return null;
} catch (IOException exception) {
exception.printStackTrace();
return null;
}
Log.i("routes",""+response);
jsonstr=response;
if(jsonstr!= null){
try{
/*JSONArray jsonary=new JSONArray(jsonstr);
JSONArray bus_scheduleee=jsonary.getJSONArray("bus_schedule");*/
JSONObject jsonObj = new JSONObject(jsonstr);
JSONArray bus_schedule = jsonObj.getJSONArray("bus_schedule");
JSONArray bus_route = jsonObj.getJSONArray("bus_route");
for (int i = 0; i < bus_route.length(); i++) {
JSONObject c = bus_route.getJSONObject(i);
String journeydatefromjson=c.getString("serviceDate");
String busname=c.getString("amenities");
String fromplace_json=c.getString("startCityName");
String departtimejson=c.getString("departureTime");
String farefromjson=c.getString("fare");
String endCityNamejson=c.getString("endCityName");
String arrivalTimejson=c.getString("arrivalTime");
String operatorNamejson=c.getString("operatorName");
String bus_idjson=c.getString("bus_id");
String journeyHoursjson=c.getString("journeyHours");
String available_seatjson=c.getString("available_seat");
String total_seatsjson=c.getString("total_seats");
String seat_typejson=c.getString("journeyHours");
Log.d("busdetails",""+journeydatefromjson+busname);
}
} catch (JSONException e) {
e.printStackTrace();
}
}
return null;
}`
答案 0 :(得分:0)
尝试使用Gson lib进行解析。
dependencies {
compile 'com.google.code.gson:gson:2.3.1'
}
你可以为parse json生成pojo。 (例如,在此网站上http://www.jsonschema2pojo.org)
对于解析使用此。
T t = new Gson().fromJson(serverResponse.getResponse(), type);
其中“T”是您的回复数据,并从网站“输入”它。
如果您的类型是实体,请使用Type type = Entity.class; 如果您的类型是实体列表,请使用
Type type = new TypeToken<List<WadadayFull>>() {
}.getType())
答案 1 :(得分:0)
试试这段代码:
try {
JSONArray array1 = new JSONArray(str);
for (int i=0;i<array1.length();i++){
JSONObject obj1 = array1.getJSONObject(i) ;
JSONArray array2 = obj1.getJSONArray("bus_schedule");
for (int j=0;j<array2.length();j++){
JSONObject obj2 = array2.getJSONObject(j);
JSONArray array3 = obj2.getJSONArray("bus_route");
ArrayList<Example1> list = new ArrayList<>();
for (int k=0;k<array3.length();k++) {
JSONObject obj3 = array3.getJSONObject(k);
String s1 = obj3.getString("serviceDate");
String s2 = obj3.getString("amenities");
String s3 = obj3.getString("startCityName");
String s4 = obj3.getString("departureTime");
String s5 = obj3.getString("fare");
String s6 = obj3.getString("endCityName");
String s7 = obj3.getString("arrivalTime");
String s8 = obj3.getString("operatorName");
String s9 = obj3.getString("bus_id");
String s10 = obj3.getString("journeyHours");
String s11 = obj3.getString("available_seat");
String s12 = obj3.getString("total_seats");
String s13 = obj3.getString("seat_type");
Example1 ex1 = new Example1();
ex1.setServiceDate(s1);
ex1.setAmenities(s2);
ex1.setStartCityName(s3);
list.add(ex1);
Log.e("response", list.toString());
}
JSONArray array4 = obj2.getJSONArray("boardingPoint");
ArrayList<Example2> list2 = new ArrayList<>();
for (int l = 0; l < array4.length(); l++) {
JSONObject obj4 = array4.getJSONObject(l);
String s14 = obj4.getString("boardingPointName");
String s15 = obj4.getString("boardingPointContact");
String s16 = obj4.getString("boardingPointTime");
String s17 = obj4.getString("boardingPointId");
String s18 = obj4.getString("BusId");
Example2 ex2 = new Example2();
ex2.setBoardingPointName(s14);
ex2.setBoardingPointContact(s15);
list2.add(ex2);
Log.e("response", list2.toString());
}
}
}
Log.e("response",array1.toString());
} catch (JSONException e) {
e.printStackTrace();
}