数据交换格式:json,xml,html
json:一种轻量级的数据交换格式,
xml:是一种标记性语言,是一种重量级的数据交换格式。
json与xml区别:
1.json占用宽带小,xml占用宽带大。
2.微服务架构,基本上都是采用http+json格式进行数据传输。
3.在配置文件中使用xml,webservice(http+http+xml)报位传输,maven配置,银行的内部接口等等。
数据交换格式有时候也被称为报位。
json解析常用的框架有:
gson(谷歌),fastjson(阿里巴巴),jackJson(SpringMVC)
/**
* json 转对象1
*/
public static void test03(){
String json ="{\"id\":1,\"name\":\"闫天蓬\",\"Adress\":{\"provice\":\"河南省\",\"city\":\"鹿邑县\",\"area\":\"王皮六镇\"},\"Hobby\":[{\"Category\":\"篮球\",\"Name\":\"运动\"},{\"Category\":\"听歌\",\"Name\":\"音乐\"}]}";
User user = JSONObject.parseObject(json, User.class);
Integer id = user.getId();
System.out.println("id: "+id);
System.out.println("Name:"+user.getName());
AdressEntity adress = user.getAdress();
System.out.println("provice: "+adress.getProvice());
System.out.println("city:"+adress.getCity());
System.out.println("area"+adress.getArea());
List<HobbyEntity> hobbys = user.getHobby();
for (HobbyEntity hobbyEntity:hobbys) {
System.out.println("Name:"+hobbyEntity.getName());
System.out.println("category"+hobbyEntity.getCategory());
}
}
public static void test02(){
String json ="{\"track\":[{\"billcode\":\"9660034086\",\"detail\":[{\"time\":\"2017-01-0621:05:13\",\"scantype\":\"收件\",\"memo\":\"HavenotbesentShenzhenHXlogistics(深圳)\",\"location\":\"ShenzhenHXlogistics(深圳)\"},{\"time\":\"2017-01-0621:11:37\",\"scantype\":\"已录包裹信息\",\"memo\":\"托运资讯发送给FedEx\",\"location\":\"\"},{\"time\":\"2017-01-0710:13:11\",\"scantype\":\"到件\",\"memo\":\"ArrivedATShenzhenOperationCentre(深圳分拨中心)\",\"location\":\"ShenzhenOperationCentre(深圳分拨中心)\"},{\"time\":\"2017-01-0711:50:59\",\"scantype\":\"出货\",\"memo\":\"ShipemtnsweresentShenzhenOperationCentre(深圳分拨中心)\",\"location\":\"ShenzhenOperationCentre(深圳分拨中心)\"},{\"time\":\"2017-01-0914:57:00\",\"scantype\":\"代理已取件\",\"memo\":\"已取件\",\"location\":\"KWUNTONGHK\"},{\"time\":\"2017-01-0920:20:00\",\"scantype\":\"代理状态\",\"memo\":\"已离开发件地FedEx站点\",\"location\":\"KWUNTONGHK\"},{\"time\":\"2017-01-1001:16:00\",\"scantype\":\"代理状态\",\"memo\":\"正在运输\",\"location\":\"LANTAUISLANDHK\"},{\"time\":\"2017-01-1018:45:00\",\"scantype\":\"代理状态\",\"memo\":\"正在运输\",\"location\":\"CHICAGO,IL\"},{\"time\":\"2017-01-1104:11:00\",\"scantype\":\"代理已取件\",\"memo\":\"到达联邦快递工作地点\",\"location\":\"MEMPHIS,TN\"},{\"time\":\"2017-01-1110:01:00\",\"scantype\":\"代理状态\",\"memo\":\"国际货物放行-进口\",\"location\":\"MEMPHIS,TN\"},{\"time\":\"2017-01-1115:23:00\",\"scantype\":\"代理状态\",\"memo\":\"离开联邦快递工作地点\",\"location\":\"MEMPHIS,TN\"},{\"time\":\"2017-01-1118:06:00\",\"scantype\":\"代理状态\",\"memo\":\"位于目的地分拣中心\",\"location\":\"BALTIMORE,MD\"},{\"time\":\"2017-01-1208:02:00\",\"scantype\":\"代理状态\",\"memo\":\"位于当地的FedEx工作地点\",\"location\":\"LINTHICUMHEIGHTS,MD\"},{\"time\":\"2017-01-1208:07:00\",\"scantype\":\"代理状态\",\"memo\":\"货件已装车,派送途中\",\"location\":\"LINTHICUMHEIGHTS,MD\"},{\"time\":\"2017-01-1209:48:00\",\"scantype\":\"签收\",\"memo\":\"已送达\",\"location\":\"JESSUP,MD\"}]}]}";
JSONObject jsonObject = JSONObject.parseObject(json);
JSONArray track = jsonObject.getJSONArray("track");
for (int i = 0; i <track.size() ; i++) {
JSONObject jsonObject1 = track.getJSONObject(i);
String billcode = jsonObject1.getString("billcode");
System.out.println("billcode:"+billcode);
System.out.println();
JSONArray detail = jsonObject1.getJSONArray("detail");
for (int j = 0; j <detail.size() ; j++) {
JSONObject detailJSONObject = detail.getJSONObject(i);
String memo = detailJSONObject.getString("memo");
System.out.println("memo:"+memo);
String location = detailJSONObject.getString("location");
System.out.println("location:"+location);
String time = detailJSONObject.getString("time");
System.out.println("time:"+time);
String scantype = detailJSONObject.getString("scantype");
System.out.println("scantype:"+scantype);
System.out.println();
}
}
}