当前位置:网站首页>利用 Fastjson json (简单粗暴版)
利用 Fastjson json (简单粗暴版)
2022-07-27 16:28:00 【黑暗料理界的扛把子】
参考博客:https://blog.csdn.net/qq877507054/article/details/51395852
先看json的样式:含有多个数组。
{
"status":0,
"message":"ok",
"total":134,
"results":[
{
"name":"北京市dnf小学",
"location":{
"lat":39.923629,
"lng":116.388059
},
"address":"府右街丙27号",
"province":"北京市",
"city":"北京市",
"area":"西城区",
"street_id":"8ca69c393be618bfc061e",
"telephone":"",
"detail":1,
"uid":"8ca69c70e393befc061e",
"detail_info":{
"tag":"教育培训;小学",
"navi_location":{
"lng":116.3887231398,
"lat":39.92324024956
},
"type":"education",
"detail_url":"",
"overall_rating":"0.0",
"children":[
]
}
},
{
"name":"dudu小学(遂安伯校区)",
"location":{
"lat":39.922193,
"lng":116.430818
},
"address":"金宝街65号",
"province":"北京市",
"city":"北京市",
"area":"东城区",
"street_id":"9bdab243406c476051",
"detail":1,
"uid":"9bda75243406c476051",
"detail_info":{
"tag":"教育培训;小学",
"navi_location":{
"lng":116.431089492,
"lat":39.9215554065
},
"type":"education",
"detail_url":"",
"children":[
]
}
}
]
}
//将上面的json 存储为 String 类型
String json = jsonlist.get(a).toString();
System.out.println("json:" + json);
//将JsonObject数据转换为Json
JSONObject object = JSON.parseObject(json);
System.out.println(object);
//获取 json 当中的 :results
Object results = object.get("results");
System.out.println("results:" + results);
//转换成数组
JSONArray array = JSON.parseArray(results.toString());
//遍历其中的数据
for(int b=0;b<array.size();b++){
System.out.println(array.get(b));
//这行必须写:必须加上+"";不然会报出类型强转异常!
String str = array.get(b)+"";
JSONObject resultsGetJson = JSON.parseObject(str);
//name
String name = resultsGetJson.get("name").toString();
System.out.println("name:" + name);
//address
String address = resultsGetJson.get("address").toString();
System.out.println("address:" + address);
//telephone 并不是每个results当中都含有telephone
String telephone = "" ;
try {
telephone = resultsGetJson.get("telephone").toString();
System.out.println("telephone:" + telephone);
} catch (Exception e) {
// TODO: handle exception
}
//获取经纬度信息
String location = resultsGetJson.get("location").toString();
JSONObject locationJson = JSON.parseObject(location);
String locationLat = locationJson.get("lat").toString();
System.out.println("locationLat:" + locationLat);
String locationLng = locationJson.get("lng").toString();
System.out.println("locationLng:" + locationLng);
String detail_info = resultsGetJson.get("detail_info").toString();
System.out.println("detail_info:" + detail_info);
//将JsonObject数据转换为Json
JSONObject detail_infoJson = JSON.parseObject(detail_info);
String tag = "" ; String navi_location = "" ; String messagelng = "" ; String messagelat = "" ;
try {
tag = detail_infoJson.get("tag").toString();
System.out.println("tag:" + tag);
navi_location = detail_infoJson.get("navi_location").toString();
JSONObject navi_locationJson = JSON.parseObject(navi_location);
messagelng = navi_locationJson.get("lng").toString();
System.out.println("messagelng:" + messagelng);
messagelat = navi_locationJson.get("lat").toString();
System.out.println("messagelat:" + messagelat);
} catch (Exception e) {
System.err.println("detail_infoJson内部分tag、lng、lat 不存在");
}
}
边栏推荐
- Selenium automated test interview questions family bucket
- Resource for NS2 beginner
- SSM integration
- Kinect for Unity3d----KinectManager
- 用函数在Excel中从文本字符串提取数字
- 进行接口测试时,连接数据库,对数据源进行备份、还原、验证操作
- WSN Journal indexed by SCI(转)
- Ruiji takeout notes
- NPM's ID card and dependence
- WinForm remove the close button in the upper right corner
猜你喜欢
随机推荐
X-shell remote connection virtual machine
ref 关键字的用法
Unity学习笔记(刚体-物理-碰撞器-触发器)
An article allows you to master threads and thread pools, and also solves thread safety problems. Are you sure you want to take a look?
v-if,v-else,v-for
正则表达式的扩展
换行问题双保险
PHP string operation
ES6 learning notes (1) - quick start
Usage of ref keyword
C interface knowledge collection suggestions collection
Hash、Set、List、Zset、BitMap、Scan
电磁场学习笔记-矢量分析和场论基础
The understanding of string in C.
功率单位(power control)
IDEA成功连接Database但不显示表怎么办
How to break the team with automated testing
Nacos基本概念和单机部署
Kinect2 for unity3d - avatardemo learning
ES6-新增方法







