java解析json格式
上一篇博客介绍了如何连接第三方http接口,返回json格式的字符串。这边文章续写接下来的解析json字符并入库。
1:由于这个json串稍微复杂一点,先上json格式
{
“dwmc”: “白银小镇(上海)文化产业有限公司”,
“wzfzr”: “陈万天”,
“listIcpGnBaxxWz”: [
{
“wzmc”: “小镇网”,
“listYm”: [
“silver-town.cn”
],
“baxh”: “沪ICP备17001840号-2”,
“listIcpGnBaxxJr”: [
{
“jrsmc”: “上海美橙科技信息发展有限公司”,
“listMc”: [
“福建”,
“重庆”,
“安徽”
]
}
]
},
{
“wzmc”: “白银小镇”,
“listYm”: [
“silverhome.cn”,
“agtown.cn”,
“silverhouse.com.cn”,
“silvertownlet.com”
],
“baxh”: “沪ICP备17001840号-1”,
“listIcpGnBaxxJr”: [
{
“jrsmc”: “上海美橙科技信息发展有限公司”,
“listMc”: [
“福建”,
“重庆”,
“安徽”
]
}
]
}
]
}
2:注释一下上面的字段
{
“dwmc”: “单位名称”,
“wzfzr”: “网站负责人”,
“listIcpGnBaxxWz”: [
{
“wzmc”: “网站名称”,
“listYm”: [
“域名”
],
“baxh”: “备案号”,
“listIcpGnBaxxJr”: [
{
“jrsmc”: “接入服务商名称”,
“listMc”: [
“接入地”
]
}
]
}
]
}
3:我想要入库的格式,每个域名要对应三个接入地
4:返回的数据和想要入库的格式都有了,接下来解析json字符串
首先在上一篇文章的基础上导入maven依赖
net.sf.json-lib
json-lib
2.4
jdk15
5:上解析代码,本文用了定时任务Scheduled,如下
package com.example.service;
import com.example.entity.BeiEntity;
import com.example.mapper.BeiMapper;
import com.example.utils.HttpsUtils;
import net.sf.json.JSONArray;
import net.sf.json.JSONObject;
import org.jsoup.Connection;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Service;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;
@Service
public class BeiService {
@Autowired
private BeiMapper beiMapper;
//每个月2号凌晨1点执行,备案数据入库
//@Scheduled(cron="0 0 1 2 * ?")
public void automaticFiling() {
List<BeiEntity> beiEntityList = new ArrayList<>();
//获取公司名
List<Map<String, Object>> name = beiMapper.getName();
for (int m = 0; m < name.size(); m++) {
//公司名
String dwmc = name.get(m).get("COMPANY").toString();
Connection.Response respon = null;
try {
respon = HttpsUtils.get("http://第三方ip:8629/BaMicroServiceV2/v1/listIcpShangHaiFatUnit?dwmc=" + dwmc);
} catch (IOException e) {
e.printStackTrace();
}
if (respon == null) {
return;
}
String body = respon.body();
if (isJson(body)) {
//转换为json串
JSONObject jsonObject = JSONObject.fromObject(body);
//单位名称
String dwmcs = jsonObject.get("dwmc").toString();
//网站负责人
String wzfzrs = jsonObject.get("wzfzr").toString();
//由于listIcpGnBaxxWz是一个object对象,所以转换为jsonArray
Object listIcpGnBaxxWz = jsonObject.get("listIcpGnBaxxWz");
JSONArray listIcpGnBaxxWzArray = JSONArray.fromObject(listIcpGnBaxxWz);
BeiEntity beiEntity = null;
for (int i = 0; i < listIcpGnBaxxWzArray.size(); i++) {
//网站名称
String wzmc = listIcpGnBaxxWzArray.getJSONObject(i).get("wzmc").toString();
//备案号
String baxh = listIcpGnBaxxWzArray.getJSONObject(i).get("baxh").toString();
Object listYm = listIcpGnBaxxWzArray.getJSONObject(i).get("listYm");
JSONArray listYmArray = JSONArray.fromObject(listYm);
//存放域名list,域名可能有多个
List<String> ymList = new ArrayList<>();
for (int j = 0; j < listYmArray.size(); j++) {
ymList.add(listYmArray.getString(j));
}
//由于listIcpGnBaxxWz里面还有一个obejct对象它listIcpGnBaxxJr,所以再转换成jsonArray
Object listIcpGnBaxxJr = listIcpGnBaxxWzArray.getJSONObject(i).get("listIcpGnBaxxJr");
JSONArray listIcpGnBaxxJrArray = JSONArray.fromObject(listIcpGnBaxxJr);
for (int g = 0; g < listIcpGnBaxxJrArray.size(); g++) {
//接入服务商名称
String jrsmc = String.valueOf(listIcpGnBaxxJrArray.getJSONObject(g).get("jrsmc"));
//接入地listMc
Object listMc = listIcpGnBaxxJrArray.getJSONObject(g).get("listMc");
JSONArray listMcArray = JSONArray.fromObject(listMc);
for (int s = 0; s < listMcArray.size(); s++) {
if (ymList != null && ymList.size() > 0) {
//遍历上面的域名list(增强for循环,快捷键 ymList.for)
for (String ymstr : ymList) {
beiEntity = new BeiEntity(dwmc, wzfzrs, wzmc,
ymstr, baxh, jrsmc, String.valueOf(listMcArray.get(s)), new Date());
beiEntityList.add(beiEntity);
}
}
}
}
}
}
}
beiMapper.insertSelective(beiEntityList);
}
//判断是否为json格式
public static boolean isJson(String body) {
try {
JSONObject.fromObject(body);
return true;
} catch (Exception e) {
return false;
}
}
}
5:每一步解析在注释中已经说明,谢谢观看