Call the interface, parse the Json string and store it in the database

Table of contents

1. Get the json string through the api interface

2. Use JSONArray and JSONObject to parse the json string

3. Store the data of the instantiated object in the database


1. Get the json string through the api interface

Obtain interface data through get (httpGet) request, and use HttpClient basically in six steps:

  1. Create an instance of HttpClient
  2. Create an instance of a connection method
  3. Call the execute method of the HttpClient instance to execute the request method
  4. read response
  5. Release the connection, regardless of the success of the execution method
    //创建httpClient实例
    CloseableHttpClient client = HttpClients.createDefault();
    //汽车之家api接口
    String apiPath = "https://www.autohome.com.cn/ashx/index/GetHomeFindCar.ashx";
    //创建get方法请求实例
    HttpGet httpGet = new HttpGet(apiPath);
    //添加表头,text/xml表示XML格式
    httpGet.addHeader("content-type","text/xml");
    //调用HttpClient实例执行GET实例,返回response
    HttpResponse response = client.execute(httpGet);
    //解析response,这个过程主要取决于获取的json格式,是一个对象还是一个数组,放到后面详解
    String result = EntityUtils.toString(response.getEntity());
    //释放连接
    response.close();
    client.close();

    Among them, we can judge the state of the response and verify whether the data is obtained. The state values ​​of the page request are: 200 request success, 303 redirection, 400 request error, 401 unauthorized, 403 access forbidden, 404 file Not found, 500 server error.(HttpStatus.OK = 200;HttpStatus.BAD_REQUEST = 400;HttpStatus.FORBIDDEN = 403;HttpStatus.NOT_FOUND = 404;HttpStatus.SERVICE_UNAVAILABLE =500)

    if (response.getStatusLine().getStatusCode() == HttpStatus.SC_OK) {
        String result = EntityUtils.toString(response.getEntity());//解析response
    }//getStatusLine()方法返回保存请求状态的StatusLine对象,getStatusCode()获取状态码

    2. Use JSONArray and JSONObject to parse the json string

        Before parsing the json string, we must first determine the format of the json string, and use different parsing methods for different formats. Here are some common json string formats, such as: value, string, array, object array Or array objects. The focus is on the use of curly braces and square brackets. Be sure to pay attention to these two symbols, which may cause json parsing errors.

//json数值
{
    "key" : 520,
    "key1" : 1314
}    
//json字符串
{
    "key" : "我爱你",
    "key1" : "一生一世"
}
//json数组
{
    "key" : [520, 1314],
    "key1" : [520, 3344]
}
//json对象数组
{
    "我" : [
                  {"key": "我爱你"},
                  {"key1": "一生一世"}
    ]
}
//json数组对象
{
    "我" : {
                  [520,1314],
                  ["我爱你", "一生一世"]
    }
}

 It can be seen that the json string obtained from Autohome is in json array format, so we need to parse it with JSONArray, then traverse the json array, obtain each json object, and then read the data from the json object.

//将json字符串解析成json数组的形式
JSONArray jsonArray = JSONArray.parseArray(result);
//利用遍历解析json数组,并在循环中解析每一个json对象
for (int i = 0; i < jsonArray.size(); i++) {
    //将json数组解析为每一个jsonObject对象
    JSONObject object=jsonArray.getJSONObject(i);
    //实例化一个dao层或者domain层的对象
    CarBrand Brand = new CarBrand();
    //将json对象中的数据写入实例化的对象中
    //注意object读取的字段要和json对象中的字段一样,否则无法解析
    Brand.setId(object.getInteger("id"));
    Brand.setName(object.getString("name"));
    Brand.setGroup(object.getString("letter"));
}

3. Store the data of the instantiated object in the database

In the springboot framework, mybatis-generator can generate domain layer entity files, xml files, mapper files and corresponding service files. Using this plug-in can save us the time of writing sql statements, and we can directly call the corresponding in the service layer method.

//调用service层的add方法,直接将实例化对象写入数据库
CarBrandService.add(Brand);

The complete code is as follows

package org.linlinjava.litemall.admin.service;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import org.apache.http.HttpResponse;
import org.apache.http.HttpStatus;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.util.EntityUtils;
import org.linlinjava.litemall.db.dao.LitemallCarBrandMapper;
import org.linlinjava.litemall.db.domain.LitemallCarBrand;
import org.linlinjava.litemall.db.service.LitemallCarBrandService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.List;
import java.util.Map;


@Service
public class AdminCarBrandService {



    @Autowired
    CarBrandService carBrandService;


    public void httpRequest() {
    
        //使用httpclient获取api数据
        CloseableHttpClient client = HttpClients.createDefault();
        String apiPath = "https://www.autohome.com.cn/ashx/index/GetHomeFindCar.ashx";
        HttpGet httpGet = new HttpGet(apiPath);
        try{
            httpGet.addHeader("content-type","text/xml");
            HttpResponse response = client.execute(httpGet);
            if (response.getStatusLine().getStatusCode() == HttpStatus.SC_OK) {
                //对获取的string数据进行json解析
                String result = EntityUtils.toString(response.getEntity());
                JSONArray jsonArray = JSONArray.parseArray(result);

                for (int i = 0; i < jsonArray.size(); i++) {
                    //将json对象中的数据写入实例化对象中
                    CarBrand carBrand = new CarBrand();
                    JSONObject object=jsonArray.getJSONObject(i);
                    carBrand.setId(object.getInteger("id"));
                    carBrand.setName(object.getString("name"));
                    carBrand.setGroup(object.getString("letter"));

                    //将实例化对象存入数据库
                    carBrandService.add(CarBrand);
                }

            }
        }catch (Exception e){
            throw new RuntimeException(e);
        }

    }
}

Record your own learning process, if there are mistakes, welcome to discuss and point out.

Guess you like

Origin blog.csdn.net/weixin_53387347/article/details/125373058