Android 如何解析以下类型的json响应?
您好,我是android新手。请建议我如何解析下面的jsonresposne并获取值。任何帮助请建议我。我不知道如何解析,因为它包含方括号和花括号Android 如何解析以下类型的json响应?,android,json,Android,Json,您好,我是android新手。请建议我如何解析下面的jsonresposne并获取值。任何帮助请建议我。我不知道如何解析,因为它包含方括号和花括号 `[ { "bus_schedule":[ { "bus_route":[ { "serviceDate":"2016-12-31", "amenities":"
`[
{
"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;
}`
`[
{
“巴士时刻表”:[
{
“巴士路线”:[
{
“服务日期”:“2016-12-31”,
“便利设施”:“VB大号半卧铺(2加1)精选WIFI、水、瓶子、毯子、零食、电影、食品、紧急出口、灭火器、公交车女服务员、闭路电视、紧急联系电话”,
“startCityName”:“Coimbatore”,
“出发时间”:“21:00:00”,
“票价”:“499”,
“endCityName”:“Kanyakumari”,
“到达时间”:“06:00:00”,
“operatorName”:“跑步赢”,
“总线id”:“17”,
“工作时间”:“9”,
“可用座位”:25,
“总席位”:34,
“座椅类型”:“半卧铺”
}
],“登机点”:[
{
“boardingPointName”:“Thudiyalur”,
“boardingPointContact”:“8883088820”,
“登机时间”:“21:25:00”,
“boardingPointId”:“316”,
“BusId”:“17”
},`
我的代码是
`受保护的Void doInBackground(Void…参数){
HttpGet请求=新的HttpGet(url);
ResponseHandler ResponseHandler=新BasicResponseHandler();
试一试{
response=mClient.execute(请求、响应句柄);
}捕获(ClientProtocolException异常){
异常。printStackTrace();
返回null;
}捕获(IOException异常){
异常。printStackTrace();
返回null;
}
Log.i(“路由”,“响应”);
jsonstr=响应;
if(jsonstr!=null){
试一试{
/*JSONArray jsonary=新JSONArray(jsonstr);
JSONArray bus_scheduleee=jsonary.getJSONArray(“bus_Schedulee”)*/
JSONObject jsonObj=新的JSONObject(jsonstr);
JSONArray总线调度=jsonObj.getJSONArray(“总线调度”);
JSONArray bus_route=jsonObj.getJSONArray(“bus_route”);
对于(int i=0;i
尝试使用Gson lib对其进行解析
dependencies {
compile 'com.google.code.gson:gson:2.3.1'
}
您可以为解析json生成pojo。(例如,在本网站上)
对于解析,请使用此
T t = new Gson().fromJson(serverResponse.getResponse(), type);
其中“T”是您的响应数据,并“键入”来自站点的pojo
如果您的类型是entity,请使用type=entity.class;
如果您的类型是实体列表,请使用
Type type = new TypeToken<List<WadadayFull>>() {
}.getType())
Type Type=new-TypeToken(){
}.getType())
尝试以下代码:
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();
}
试试看{
JSONArray array1=新JSONArray(str);
对于(int i=0;i)您的描述(“方括号和花括号”)表明您可能不理解JSON是什么。如果您能够解释方括号和花括号在JSON中的含义,那很好,请继续。如果不能,您应该在继续之前先了解它,否则每次尝试使用它时都会遇到问题。