我是Android Studio的初学者,在Android中解析json数据有点困难,所以我想问有关获取或解析JSON Child Array的问题。
这是我的代码:
public void resSuccess(String requestType, JSONObject response)
{ progressBar.setVisibility(View.GONE);
try {
token = response.getString("token");
JSONArray array = response.getJSONArray("all_airport");
for (int i=0; i<array.length(); i++){
JSONObject jsonObject = array.getJSONObject(i);
JSONArray jsonArray = jsonObject.getJSONArray("airport");
for (int j=0; j<jsonArray.length(); j++) {
JSONObject object = jsonArray.getJSONObject(j);
BandaraDataSet bds = new BandaraDataSet();
idDep = object.getString("country_id");
bds.setId(object.getString("id"));
bds.setAirport_name(object.getString("airport_name"));
bds.setAirport_code(object.getString("airport_code"));
bds.setCountry_name(object.getString("country_name"));
bds.setCountry_id(object.getString("country_id"));
bds.setLocation_name(object.getString("location_name"));
list.add(bds);
}
}
bandaraAdapter = new BandaraAdapter(ActivityPesawat.this, list);
bandaraAdapter.notifyDataSetChanged();
listBandara.setAdapter(bandaraAdapter);
} catch (Exception e){
e.printStackTrace();
}
}
这是我的杰森
{
"all_airport":{
"airport":[
{
"airport_name":"Mali",
"airport_code":"ARD",
"location_name":"Alor Island",
"country_id":"id",
"country_name":"Indonesia"
},
{
"airport_name":"Pattimura",
"airport_code":"AMQ",
"location_name":"Ambon",
"country_id":"id",
"country_name":"Indonesia"
},
{
"airport_name":"Tanjung Api",
"airport_code":"VPM",
"location_name":"Ampana",
"country_id":"id",
"country_name":"Indonesia"
}
]
},
"token":"ab4f5e12e794ab09d49526bc75cf0a0139d9d849",
"login":"false"
}
所以当Android中Parse Json为null时出现我的问题,请帮助任何人。
答案 0 :(得分:0)
您应该正确解析json,否则您可以使用Gson(library)进行json解析。您只需要具有适当的Model(Bean)类。然后解析太简单了。
编译'com.google.code.gson:gson:2.7'
在下面的Model / Bean类中创建
import java.io.Serializable;
//Response.java
public class Response implements Serializable {
AllAirPort all_airport;
public AllAirPort getAll_airport() {
return all_airport;
}
public void setAll_airport(AllAirPort all_airport) {
this.all_airport = all_airport;
}
}
//AllAirPort.java
public class AllAirPort implements Serializable{
ArrayList<AirportModel> airport;
public ArrayList<AirportModel> getAirport() {
return airport;
}
public void setAirport(ArrayList<AirportModel> airport) {
this.airport = airport;
}
}
//AirportModel.java
public class AirportModel implements Serializable {
String airport_name;
String airport_code;
String location_name;
String country_id;
String country_name;
public String getAirport_name() {
return airport_name;
}
public void setAirport_name(String airport_name) {
this.airport_name = airport_name;
}
public String getAirport_code() {
return airport_code;
}
public void setAirport_code(String airport_code) {
this.airport_code = airport_code;
}
public String getLocation_name() {
return location_name;
}
public void setLocation_name(String location_name) {
this.location_name = location_name;
}
public String getCountry_id() {
return country_id;
}
public void setCountry_id(String country_id) {
this.country_id = country_id;
}
public String getCountry_name() {
return country_name;
}
public void setCountry_name(String country_name) {
this.country_name = country_name;
}
}
Response responseObject = new Gson()。fromJson(yourstringResponse,Response.class);
现在您可以开始从responseObject获取数据了。
答案 1 :(得分:0)
您正在像处理dicts = {
'd1': {'goober': True, 'horse': 42},
'd2': {'goober': True, 'horse': 42},
'd3': {'goober': False, 'horse': 71}
}
desired_dict = {}
group_num = 0
for d in dicts:
for dd in desired_dict: # check if the dict exists in out desired_dict
if desired_dict[dd]['dict'] == dicts[d]: # if yes, just update the keys
desired_dict[dd].get('keys', []).append(d)
break
else:
group_num += 1 # else make a new dictionary group
group_name = 'group{0}'.format(group_num)
desired_dict[group_name] = {'dict': dicts[d], 'keys': [d, ]}
print(desired_dict)
Out: {'group1': {'dict': {'goober': True, 'horse': 42}, 'keys': ['d1', 'd2']},
'group2': {'dict': {'goober': False, 'horse': 71}, 'keys': ['d3']}}
一样处理JSONObject
。尝试以下代码:
JSONArray