我的Json数据如下: 这个Json数据中有两个List,于是我定义了一个类: public List<Cars> GetCars(){ public void SetDepartment(List<Departments> departments){ public List<Departments> GetDepartment(){ 解析该数据的代码如下: Data.cars.get(0);//想获得列表中第一个cars的数据,但是程序运行到这里是,直接跳过! |
|
5分
#1 |
分析下节点:
jsonObject —> data —> jsonObject —>cars —> jsonArray( List<对象> ) cars —> department—> jsonArray( List<对象> ) 依次给出几点回答: 4. 遍历取值: 注:在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<Departments> GetDepartment(){ public class Cars { private String coordinate; public void SetCoordinate(String coordinate){ public void SetStatus(String status){ public void SetDriver(String carId){ public class Departments { public void setCoordinate(String string){ public class CarList { public void setCarId(String carId){ 当我高兴的把JasonBean写出来后,赶快用Gson解析, |
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); 解析出来的有异曲同工之妙,再次感谢! |