public class jsonParse{
class City{
int id;
String name;
String code;
String map;
}
public static void main(String[] args) {
//列表/array 数据
String str="[{'id': '1','code': 'bj','name': '北京','map': '39.90403, 116.40752599999996'}, {'id': '2','code': 'sz','name': '深圳','map': '22.543099, 114.05786799999998'}, {'id': '9','code': 'sh','name': '上海','map': '31.230393,121.473704'}, {'id': '10','code': 'gz','name': '广州','map': '23.129163,113.26443500000005'}]";
Gson gson=new Gson();
List<City> rs=new ArrayList<City>();
Type type = new TypeToken<ArrayList<City>>() {}.getType();
rs=gson.fromJson(str, type);
for(City o:rs){
System.out.println(o.name);
}
//map数据
String jsonStr="{'1': {'id': '1','code': 'bj','name': '北京','map': '39.90403, 116.40752599999996'},'2': {'id': '2','code': 'sz','name': '深圳','map': '22.543099, 114.05786799999998'},'9': {'id': '9','code': 'sh','name': '上海','map': '31.230393,121.473704'},'10': {'id': '10','code': 'gz','name': '广州','map': '23.129163,113.26443500000005'}}";
Map<String, City> citys = gson.fromJson(jsonStr, new TypeToken<Map<String, City>>() {}.getType());
System.out.println(citys.get("1").name+"----------"+citys.get("2").code);
}
}
转
http://blog.csdn.net/onlyonecoder/article/details/16907713
Gson解析(List和Map)格式json数据
最新推荐文章于 2023-05-17 11:01:18 发布