通过JAVA从接口获取json数据 解析并存入数据库的程序实例
json
适用于现代 C++ 的 JSON。
项目地址:https://gitcode.com/gh_mirrors/js/json
免费下载资源
·
1.java代码,结构如下入
1.pom.xml文件
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>tcl.ln.geturl</groupId>
<artifactId>geturl</artifactId>
<version>1.0-SNAPSHOT</version>
<dependencies>
<!--map和json数据交换转换-->
<dependency>
<groupId>net.sf.json-lib</groupId>
<artifactId>json-lib</artifactId>
<version>2.4</version>
<classifier>jdk15</classifier>
</dependency>
<!--mysql驱动jar包 -->
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>5.1.25</version>
</dependency>
</dependencies>
<!--Spring Boot可以以jar包的形式独立运行。指定运行主类 -->
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<configuration>
<mainClass>geturl.Iputil</mainClass>
</configuration>
<executions>
<execution>
<goals>
<goal>repackage</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
</build>
</project>
2. 通过此数据建立实体类
作用:定义json封装的bean对象
package geturl;
import java.util.List;
public class JsonBean {
private String status;
private String msg;
// 封装data的数据
private DataBean data;
public String getStatus() {
return status;
}
public void setStatus(String status) {
this.status = status;
}
public String getMsg() {
return msg;
}
public void setMsg(String msg) {
this.msg = msg;
}
public DataBean getData() {
return data;
}
public void setData(DataBean data) {
this.data = data;
}
//Data数据封装
public static class DataBean {
private Integer totalSize;
private Integer hasNext;
// 封装tuples的数据
private List<TuplesBean> list;
public Integer getTotalSize() {
return totalSize;
}
public void setTotalSize(Integer totalSize) {
this.totalSize = totalSize;
}
public Integer getHasNext() {
return hasNext;
}
public void setHasNext(Integer hasNext) {
this.hasNext = hasNext;
}
public List<TuplesBean> getList() {
return list;
}
public void setList(List<TuplesBean> list) {
this.list = list;
}
}
//Tuples数据封装
public static class TuplesBean {
private String id;
private String sn;
private String activationCode;
private String source;
private String phoneNum;
private String ip;
private String status;
private String obtainTime;
private String synTime;
public TuplesBean() {
}
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
public String getSn() {
return sn;
}
public void setSn(String sn) {
this.sn = sn;
}
public String getActivationCode() {
return activationCode;
}
public void setActivationCode(String activationCode) {
this.activationCode = activationCode;
}
public String getSource() {
return source;
}
public void setSource(String source) {
this.source = source;
}
public String getIp() {
return ip;
}
public void setIp(String ip) {
this.ip = ip;
}
public String getStatus() {
return status;
}
public void setStatus(String status) {
this.status = status;
}
public String getObtainTime() {
return obtainTime;
}
public void setObtainTime(String obtainTime) {
this.obtainTime = obtainTime;
}
public String getSynTime() {
return synTime;
}
public void setSynTime(String synTime) {
this.synTime = synTime;
}
public String getPhoneNum() {
return phoneNum;
}
public void setPhoneNum(String phoneNum) {
this.phoneNum = phoneNum;
}
}
}
3.数据库连接类
package geturl;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
public class Conn {
//数据库连接类
/**
* @return Connection
* @method getConn()
*/
public Connection getConn() {
String driver = "com.mysql.jdbc.Driver";
String url = "jdbc:mysql://101.68.133.151:3306/tcl_adwdfa";
String username = "ddadad";
String password = "dsadaw";
Connection conn = null;
try {
Class.forName(driver); //
conn = (Connection) DriverManager.getConnection(url, username, password);
System.out.println("数据库连接成功");
} catch (ClassNotFoundException e) {
e.printStackTrace();
} catch (SQLException e) {
e.printStackTrace();
}
return conn;
}
}
4.解析存储类
package geturl;
import java.net.*;
import java.security.MessageDigest;
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.Statement;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.*;
import java.io.*;
import net.sf.json.JSONArray;
import net.sf.json.JSONObject;
//解析存储类
public class Iputil {
// 记录请求的json的页数
static int count = 1;
// 记录日志的日期
static String date ;
public static void main(String[] args) throws ParseException {
//请求得到json,有多少页执行多少次
for (int i=1;i<=count ;i++){
// 得到执行日期前一天的日期
Calendar calendar = Calendar.getInstance();
SimpleDateFormat sj = new SimpleDateFormat("yyyyMMdd");
calendar.add(Calendar.DATE, -1);//当前时间减去一年,即一年前的时间
String str = sj.format(calendar.getTime());
// 将日期赋值给data,run方法需要用
date = str ;
// 用md5 信息摘要算法解密
String token = md5(str + "hyugtf@tcl.com");
// post请求的参数
HashMap<String, String> map = new HashMap<>();
map.put("date", str);
map.put("pageSize", "1000");
map.put("pageNum", i+"");
map.put("token", token);
Iputil iputil = new Iputil();
iputil.run(map);
}
}
//md5 信息摘要算法
public static String md5(String source) {
StringBuffer sb = new StringBuffer(32);
try {
MessageDigest md = MessageDigest.getInstance("MD5");
byte[] array = md.digest(source.getBytes("utf-8"));
for (int i = 0; i < array.length; i++) {
sb.append(Integer.toHexString((array[i] & 0xFF) | 0x100).toUpperCase().substring(1, 3));
}
} catch (Exception e) {
}
return sb.toString();
}
//向接口请求数据后封装后写入数据库
public void run(Map<String, String> params) {
JsonBean jsonBean = new JsonBean();
// post请求参数拼接
StringBuffer sb = new StringBuffer();
if (params != null) {
for (Map.Entry<String, String> e : params.entrySet()) {
sb.append(e.getKey());
sb.append("=");
sb.append(e.getValue());
sb.append("&");
}
//去掉最后一个&
sb.substring(0, sb.length() - 1);
}
try {
URL url = new URL("https://tvactivity.api.my7v.com/auddath/activity/dafa");
URLConnection urlConnection = url.openConnection();
HttpURLConnection connection = null;
if (urlConnection instanceof HttpURLConnection) {
connection = (HttpURLConnection) urlConnection;
} else {
System.out.println("输入urlַ");
return;
}
connection.setRequestMethod("POST");
connection.setDoOutput(true);
connection.setDoInput(true);
connection.setUseCaches(false);
connection.setRequestProperty("Content-Type", "application/x-www-form-urlencoded");
OutputStreamWriter osw = new OutputStreamWriter(connection.getOutputStream(), "UTF-8");
osw.write(sb.toString());
osw.flush();
osw.close();
BufferedReader in = new BufferedReader(
new InputStreamReader(connection.getInputStream()));
// 存储得到的json
String urlString = "";
String current;
while ((current = in.readLine()) != null) {
urlString += current;
}
//将json解析为JSONObject对象
JSONObject json = JSONObject.fromObject(urlString);
// 从json对象中获取数据存入jsonBean对象中
jsonBean.setStatus(json.optString("status"));
jsonBean.setMsg(json.optString("msg"));
// 得到date仍然是json对象,需要进一步封装
JSONObject dataObj = json.optJSONObject("data");
JsonBean.DataBean data = new JsonBean.DataBean();
data.setHasNext(dataObj.optInt("hasNext"));
data.setTotalSize(dataObj.optInt("totalSize"));
// 更改count的次数,每页1000条
count = (data.getTotalSize()/1000+1);
// 将tuples直接封装为JSONArray
JSONArray tuplesArray = dataObj.optJSONArray("tuples");
List<JsonBean.TuplesBean> tuplesBeansList = new ArrayList<>();
//进一步封装tuples的数据
for (int i = 0; i < tuplesArray.size(); i++) {
JSONObject tuplesObj = tuplesArray.optJSONObject(i);
JsonBean.TuplesBean tuplesBean = new JsonBean.TuplesBean();
tuplesBean.setId(tuplesObj.getString("id"));
tuplesBean.setSn(tuplesObj.getString("sn"));
tuplesBean.setActivationCode(tuplesObj.getString("activationCode"));
tuplesBean.setSource(tuplesObj.getString("source"));
try {
tuplesBean.setPhoneNum(tuplesObj.getString("phoneNum"));
} catch (Exception e){
tuplesBean.setPhoneNum("");
}
tuplesBean.setIp(tuplesObj.getString("ip"));
tuplesBean.setStatus(tuplesObj.getString("status"));
tuplesBean.setObtainTime(tuplesObj.getString("obtainTime"));
tuplesBean.setSynTime(tuplesObj.getString("synTime"));
// tuplesBean增加到tuplesBeansList
tuplesBeansList.add(tuplesBean);
}
// tuplesBeansList增加到data
data.setList(tuplesBeansList);
// data增加到jsonBean
jsonBean.setData(data);
} catch (IOException e) {
e.printStackTrace();
} //get数据
Conn c = new Conn(); //连接数据库
Connection con = c.getConn();
try {
Statement sql;
ResultSet res;
int a;
sql = con.createStatement();
/* sql.executeQuery("CREATE TABLE Map (id varchar(255), name varchar(255),tag varchar(255),"
+ "type varchar(255),typecode varchar(255), biz_type varchar(255),address varchar(255),location varchar(255),"
+ "tel varchar(255),postcode varchar(255),website varchar(255),email varchar(255),pcode varchar(255),pname varchar(255),"
+ "citycode varchar(255), cityname varchar(255), adcode varchar(255),adname varchar(255), importance varchar(255),shopid varchar(255),shopinfo varchar(255),poiweight varchar(255),"
+ "gridcode varchar(255),distance varchar(255),navi_poiid varchar(255),entr_location varchar(255),business_area varchar(255),exit_location varchar(255),"
+ "match varchar(255),recommend varchar(255),timestamp varchar(255),alias varchar(255),indoor_map varchar(255),indoor_data varchar(255),"
+ "groupbuy_num varchar(255),discount_num varchar(255),biz_ext varchar(255),event varchar(255),children varchar(255),photos varchar(400)) ");
*/
//从jsonBean中获取封装的数据插入数据库中,得道的每条的数据都插入
for (int i = 0; i < jsonBean.getData().getList().size(); i++) {
a = sql.executeUpdate("insert into huannet_dzbd_info_inc_daily (sn,activationCode,source,phoneNum,ip,status,obtainTime,synTime,date)"
+ "values('" + jsonBean.getData().getList().get(i).getSn() + "','"
+ jsonBean.getData().getList().get(i).getActivationCode() + "','" + jsonBean.getData().getList().get(i).getSource() + "','"+ jsonBean.getData().getList().get(i).getPhoneNum() +"','"+ jsonBean.getData().getList().get(i).getIp() + "'"
+ ",'" + jsonBean.getData().getList().get(i).getStatus() + "','" + jsonBean.getData().getList().get(i).getObtainTime() + "','" + jsonBean.getData().getList().get(i).getSynTime() + "','" + date + "')");
}
} catch (Exception e) {
e.printStackTrace();
}
}
}
2.将打好的jar上传到服务器
1.打包步骤
点击idea的Maven后,点击package。如下图
2.执行打包后的jar
java -jar geturl-1.0-SNAPSHOT.jar
GitHub 加速计划 / js / json
41.72 K
6.61 K
下载
适用于现代 C++ 的 JSON。
最近提交(Master分支:1 个月前 )
960b763e
4 个月前
8c391e04
6 个月前
更多推荐
已为社区贡献3条内容
所有评论(0)