支持
API数据类型
{
"code": 200,
"message": "SUCCESS",
"data": [
{
"date": "01.20",
"country": "中国",
"newConfirm": 0,
"description": "",
"dead": 0,
"confirm_cuts": "",
"confirm_add": "0",
"confirm": 0,
"dead_cuts": "",
"newDead": 0,
"province": "台湾",
"heal": 0,
"now_confirm_cuts": "",
"heal_cuts": "",
"newHeal": 0
},
............
]
分析
定义所需变量
int i;
URL url;
URLConnection conn;
InputStream in;
InputStreamReader isr;
BufferedReader br;
String line,str,string;
StringBuffer sb;
JSONObject jsonObject,JO;
JSONArray jsonArray;
String dead="",date="",heal="",confirm="",province="",now_confirm="",city="";
SQL sql=new SQL();
从API中获取数据并写入JSON字符串
path="https://wuliang.art/ncov/statistics/getProvinceHistoryList?provinceName=%E5%8F%B0%E6%B9%BE";
url=new URL(path);
conn=url.openConnection();
in=conn.getInputStream();
isr = new InputStreamReader(in,"UTF-8");
br = new BufferedReader(isr);
line = null;
sb=new StringBuffer();
while ((line=br.readLine())!=null) {
sb.append(line);
}
str=sb.toString();
JSON字符串转化为数据录入数据库
jsonObject=JSONObject.fromObject(str);
jsonArray = jsonObject.getJSONArray("data");
string="insert into province(province,date,confirm,now_confirm,heal,dead) values ";
sb=new StringBuffer();
sb.append(string);
for (i=0;i<jsonArray.size();i++){
JO = jsonArray.getJSONObject(i);
date = JO.getString("date");
heal = JO.getString("heal");
dead = JO.getString("dead");
confirm = JO.getString("confirm");
province = JO.getString("province");
now_confirm = Integer.toString(Integer.parseInt(confirm)-Integer.parseInt(heal)-Integer.parseInt(dead));
sb.append("('");
sb.append(province);
sb.append("','");
sb.append(date);
sb.append("','");
sb.append(confirm);
sb.append("','");
sb.append(now_confirm);
sb.append("','");
sb.append(heal);
sb.append("','");
sb.append(dead);
if(i<(jsonArray.size()-1))sb.append("'),");
else sb.append("')");
}
string=sb.toString();
sql.insert(string);
完整代码
int i;
URL url;
URLConnection conn;
InputStream in;
InputStreamReader isr;
BufferedReader br;
String line,str,string;
StringBuffer sb;
JSONObject jsonObject,JO;
JSONArray jsonArray;
String dead="",date="",heal="",confirm="",province="",now_confirm="",city="";
SQL sql=new SQL();
try {
path="https://wuliang.art/ncov/statistics/getProvinceHistoryList?provinceName=%E5%8F%B0%E6%B9%BE";
url=new URL(path);
conn=url.openConnection();
in=conn.getInputStream();
isr = new InputStreamReader(in,"UTF-8");
br = new BufferedReader(isr);
line = null;
sb=new StringBuffer();
while ((line=br.readLine())!=null) {
sb.append(line);
}
str=sb.toString();
jsonObject=JSONObject.fromObject(str);
jsonArray = jsonObject.getJSONArray("data");
string="insert into province(province,date,confirm,now_confirm,heal,dead) values ";
sb=new StringBuffer();
sb.append(string);
for (i=0;i<jsonArray.size();i++){
JO = jsonArray.getJSONObject(i);
date = JO.getString("date");
heal = JO.getString("heal");
dead = JO.getString("dead");
confirm = JO.getString("confirm");
province = JO.getString("province");
now_confirm = Integer.toString(Integer.parseInt(confirm)-Integer.parseInt(heal)-Integer.parseInt(dead));
sb.append("('");
sb.append(province);
sb.append("','");
sb.append(date);
sb.append("','");
sb.append(confirm);
sb.append("','");
sb.append(now_confirm);
sb.append("','");
sb.append(heal);
sb.append("','");
sb.append(dead);
if(i<(jsonArray.size()-1))sb.append("'),");
else sb.append("')");
}
string=sb.toString();
sql.insert(string);
} catch (MalformedURLException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
sql.close();
}