Gson解析复杂的Json数据

Android 码拜 9年前 (2015-07-18) 1656次浏览

我的Json数据如下:
{“data”:{“cars”:[{“coordinate”:”120.1540000,30.3140000″,”driver”:”driver”,”id”:589,”license”:”ABCDEFG9″,”speed”:40,”status”:0},{“coordinate”:”120.0827000,30.3136000″,”driver”:”driver”,”id”:591,”license”:”ABCDEFG11″,”speed”:40,”status”:0},{“coordinate”:”120.3500000,30.3200000″,”driver”:”driver”,”id”:587,”license”:”ABCDEFG7″,”speed”:40,”status”:0}],”department”[{“name”:a},{“name”:b},{“name”:c}]}}

这个Json数据中有两个List,于是我定义了一个类:
public class  JasonData {
public List<Cars> cars;
public List<Departments> departments;
public void SetCars(List<Cars> cars){
this.cars=cars;
}

public List<Cars> GetCars(){
return this.cars;
}

public void SetDepartment(List<Departments> departments){
this.departments=departments;
}

public List<Departments> GetDepartment(){
return this.departments;
}
}

解析该数据代码如下:
Gson gson=new Gson();
java.lang.reflect.Type type = new TypeToken<FirstData>() {}.getType();
JasonData Data=gson.fromJson(response, type);//response为返回的Jason数据

Data.cars.get(0);//想获得列表中第一个cars的数据,但是程序运行到这里是,直接跳过!
问题:该怎么访问Data中的list中的数据

5分

#1

分析下节点:
jsonObject —> data —> jsonObject —>cars —> jsonArray( List<对象> )
cars —> department—> jsonArray( List<对象> )

依次给出几点回答:
1.当然得需要一个Cars类和一个Departments,然后再是兄台上面的JasonData类
2. response数据有没有为空的情况
3.解析后 Data 中是否有数据

4. 遍历取值:
for(int i=0;i<Data.getCarList().size();i++){
Log.i(“GAO”, Data.getCarList().get(i).getName());
}

注:在bean中生成toString方法后,可直接 Log.i(“GAO”, Data.toString());

2分

#2

很简单,显然你已经把Json解析成对象啦,那你就用GET方法把list集合拿出来,再去遍历拿出来的list用for或foreach都行

#3

实际要解析的Jason数据是这样的
{“data”:{“cars”:[{“coordinate”:”120.1540000,30.3140000″,”driver”:”driver”,”id”:589,”license”:”ABCDEFG9″,”speed”:40,”status”:0},{“coordinate”:”120.0827000,30.3136000″,”driver”:”driver”,”id”:591,”license”:”ABCDEFG11″,”speed”:40,”status”:0},{“coordinate”:”120.3500000,30.3200000″,”driver”:”driver”,”id”:587,”license”:”ABCDEFG7″,”speed”:40,”status”:0},{“coordinate”:”120.1130000,30.4820000″,”driver”:”driver”,”id”:608,”license”:”ABCDEFG28″,”speed”:40,”status”:0},{“coordinate”:”120.1530000,30.4800000″,”driver”:”driver”,”id”:597,”license”:”ABCDEFG17″,”speed”:40,”status”:0},{“coordinate”:”120.3500000,30.3130000″,”driver”:”driver”,”id”:585,”license”:”ABCDEFG5″,”speed”:40,”status”:0},{“coordinate”:”120.3030000,30.1120000″,”driver”:”driver”,”id”:606,”license”:”ABCDEFG26″,”speed”:40,”status”:0},{“coordinate”:”120.2330000,30.3200000″,”driver”:”driver”,”id”:600,”license”:”ABCDEFG20″,”speed”:40,”status”:0},{“coordinate”:”120.4700000,30.3110000″,”driver”:”driver”,”id”:588,”license”:”ABCDEFG8″,”speed”:40,”status”:0},{“coordinate”:”120.5080000,30.3101000″,”driver”:”driver”,”id”:602,”license”:”ABCDEFG22″,”speed”:40,”status”:0},{“coordinate”:”120.2800000,30.4350000″,”driver”:”driver”,”id”:580,”license”:”ABCDEFG0″,”speed”:40,”status”:0},{“coordinate”:”120.0470000,30.0440000″,”driver”:”driver”,”id”:592,”license”:”ABCDEFG12″,”speed”:40,”status”:0},{“coordinate”:”120.3230000,30.1600000″,”driver”:”driver”,”id”:598,”license”:”ABCDEFG18″,”speed”:40,”status”:0},{“coordinate”:”120.2330000,30.1300000″,”driver”:”driver”,”id”:596,”license”:”ABCDEFG16″,”speed”:40,”status”:0},{“coordinate”:”120.2130000,30.4120000″,”driver”:”driver”,”id”:609,”license”:”ABCDEFG29″,”speed”:40,”status”:0},{“coordinate”:”120.1600000,30.2550000″,”driver”:”driver”,”id”:583,”license”:”ABCDEFG3″,”speed”:40,”status”:0},{“coordinate”:”120.1730000,30.2100000″,”driver”:”driver”,”id”:594,”license”:”ABCDEFG14″,”speed”:40,”status”:0},{“coordinate”:”120.3030000,30.2720000″,”driver”:”driver”,”id”:604,”license”:”ABCDEFG24″,”speed”:40,”status”:0},{“coordinate”:”120.4030000,30.1094000″,”driver”:”driver”,”id”:593,”license”:”ABCDEFG13″,”speed”:40,”status”:0},{“coordinate”:”120.1430000,30.2120000″,”driver”:”driver”,”id”:605,”license”:”ABCDEFG25″,”speed”:40,”status”:0},{“coordinate”:”120.2400000,30.3070000″,”driver”:”driver”,”id”:582,”license”:”ABCDEFG2″,”speed”:40,”status”:0},{“coordinate”:”120.0530000,30.2820000″,”driver”:”driver”,”id”:607,”license”:”ABCDEFG27″,”speed”:40,”status”:0},{“coordinate”:”120.1630000,30.3000000″,”driver”:”driver”,”id”:599,”license”:”ABCDEFG19″,”speed”:40,”status”:0},{“coordinate”:”120.5738000,30.2060000″,”driver”:”driver”,”id”:586,”license”:”ABCDEFG6″,”speed”:40,”status”:0},{“coordinate”:”120.1330000,30.0120000″,”driver”:”driver”,”id”:603,”license”:”ABCDEFG23″,”speed”:40,”status”:0},{“coordinate”:”120.1450000,30.3600000″,”driver”:”driver”,”id”:581,”license”:”ABCDEFG1″,”speed”:40,”status”:0},{“coordinate”:”120.1460000,30.2190000″,”driver”:”driver”,”id”:595,”license”:”ABCDEFG15″,”speed”:40,”status”:0},{“coordinate”:”120.2250000,30.3038000″,”driver”:”driver”,”id”:601,”license”:”ABCDEFG21″,”speed”:40,”status”:0},{“coordinate”:”120.0870000,30.3100000″,”driver”:”driver”,”id”:590,”license”:”ABCDEFG10″,”speed”:40,”status”:0},{“coordinate”:”120.3300000,30.2500000″,”driver”:”driver”,”id”:584,”license”:”ABCDEFG4″,”speed”:40,”status”:0}],”departments”:[{“cars”:[],”coordinate”:”120.5600000,30.2300000″,”name”:”testdepartment”},{“cars”:[],”coordinate”:”120.3300000,30.5200000″,”name”:”adddeptest”},{“cars”:[{“id”:608,”license”:”ABCDEFG28″},{“id”:609,”license”:”ABCDEFG29″},{“id”:606,”license”:”ABCDEFG26″},{“id”:607,”license”:”ABCDEFG27″}],”coordinate”:”120.1326000,30.1441000″,”name”:”test4″},{“cars”:[{“id”:593,”license”:”ABCDEFG13″},{“id”:594,”license”:”ABCDEFG14″},{“id”:592,”license”:”ABCDEFG12″},{“id”:591,”license”:”ABCDEFG11″}],”coordinate”:”120.1337000,30.0765000″,”name”:”test2″},{“cars”:[{“id”:600,”license”:”ABCDEFG20″},{“id”:590,”license”:”ABCDEFG10″},{“id”:598,”license”:”ABCDEFG18″},{“id”:596,”license”:”ABCDEFG16″},{“id”:595,”license”:”ABCDEFG15″},{“id”:585,”license”:”ABCDEFG5″},{“id”:605,”license”:”ABCDEFG25″},{“id”:597,”license”:”ABCDEFG17″},{“id”:599,”license”:”ABCDEFG19″}],”coordinate”:”120.0303000,30.8620000″,”name”:”test3″},{“cars”:[{“id”:602,”license”:”ABCDEFG22″},{“id”:601,”license”:”ABCDEFG21″},{“id”:603,”license”:”ABCDEFG23″},{“id”:580,”license”:”ABCDEFG0″},{“id”:604,”license”:”ABCDEFG24″}],”coordinate”:”120.1922000,30.3954000″,”name”:”test5″},{“cars”:[{“id”:581,”license”:”ABCDEFG1″},{“id”:583,”license”:”ABCDEFG3″},{“id”:582,”license”:”ABCDEFG2″},{“id”:584,”license”:”ABCDEFG4″}],”coordinate”:”120.1841000,30.4317000″,”name”:”test0″},{“cars”:[{“id”:589,”license”:”ABCDEFG9″},{“id”:588,”license”:”ABCDEFG8″},{“id”:587,”license”:”ABCDEFG7″},{“id”:586,”license”:”ABCDEFG6″}],”coordinate”:”120.5871000,30.6613000″,”name”:”test1″}]}}

我自己定义的JasonBean如下:

public class JasonBean {
public List<Cars> cars;
public List<Departments> departments;
public void SetCars(List<Cars> cars){
this.cars=cars;
}
public List<Cars> GetCars(){
return this.cars;
}
public void SetDepartment(List<Departments> departments){
this.departments=departments;
}

public List<Departments> GetDepartment(){
return this.departments;
}
}

public class Cars {

private String coordinate;
private String driver;
private String id;
private String license;
private String speed;
private String status;
public void SetSpeed(String speed){
this.speed=speed;
}
public String GetSpeed(){
return this.speed;
}
public void SetCarId(String carId){
this.id=carId;
}
public String GetCarId(){
return this.id;
}

public void SetCoordinate(String coordinate){
this.coordinate=coordinate;
}
public String GetCoordinate(){
return this.coordinate;
}

public void SetStatus(String status){
this.status=status;
}
public String GetStatus(){
return this.status;
}

public void SetDriver(String carId){
this.driver=driver;
}
public String GetDriver(){
return this.driver;
}
public void SetLicense(String license)
{
this.license=license;
}
public String GetLisence(){
return this.license;
}
}

public class Departments {
List<CarList> cars;
String coordinate;
String name;
public void setName(String name){
this.name=name;
}
public String getName(){return this.name;}

public void setCoordinate(String string){
this.coordinate=string;
}
public String getCoordinate(){
return this.coordinate;
}
public void setCars(List<CarList> cars){
this.cars=cars;
}
public List<CarList> getCars(){
return this.cars;
}
}

public class CarList  {
private String carId;
private String license;

public void setCarId(String carId){
this.carId=carId;
}
public String getCarId(){
return this.carId;
}
public void setLicense(String license)
{
this.license=license;
}
public String getLisence(String license){
return this.license;
}
}

当我高兴的把JasonBean写出来后,赶快用Gson解析,
Gson gson=new Gson();
java.lang.reflect.Type type=new TypeToken<FirstData>(){}.getType();
JasonBean     Data=gson.fromJson(response,type);
经过调试发现Data为空!但是response返回来Jason数据!
问题:解析出现为空,肯定是我解析错误,是我JasonBean写错了吗?

2分

#4

我的情况跟楼主类似,现在直接自己解析了没用Gson,如果楼主解决,请说一下原因,谢谢
31分

#5

再定义一个实体Data
class Data {
JasonBean  data;
}
Data data=gson.fromJson(response,Data.class);
另外CarList 实体里面的carId应为id,和json名字要对应

#6

亲测有用!谢谢亲!我觉得这和我用的
java.lang.reflect.Type type=new
TypeToken< Map<String,JasonData>>(){}.getType();
Map<String,JasonData>=gson.from(response,type);
解析出来的有异曲同工之妙,再次感谢!

CodeBye 版权所有丨如未注明 , 均为原创丨本网站采用BY-NC-SA协议进行授权 , 转载请注明Gson解析复杂的Json数据
喜欢 (0)
[1034331897@qq.com]
分享 (0)