在最近上线的小圈子Springboot项目中,使用了阿里巴巴开源fastjson对api中的数据进行解析。

引入依赖

<dependency>
    <groupId>org.apache.httpcomponents</groupId>
    <artifactId>httpclient</artifactId>
    <version>RELEASE</version>
</dependency>
<!-- fastJson -->
<dependency>
    <groupId>com.alibaba</groupId>
    <artifactId>fastjson</artifactId>
    <version>RELEASE</version>
</dependency>
<dependency>
    <groupId>commons-io</groupId>
    <artifactId>commons-io</artifactId>
    <version>RELEASE</version>
</dependency>

后端代码

创建Service

创建HttpClient Service,用来处理对外部url的http请求

@Service
public class HttpClient {
    public String client(String url, HttpMethod method, MultiValueMap<String,String> param) throws Exception{
        RestTemplate restTemplate = new RestTemplate();
        ResponseEntity<String > responseEntity = restTemplate.getForEntity(url,String.class);
        return responseEntity.getBody();
    }
}

处理url的json数据并转换成字符串

String url = "https://circleapi.url.cn/xxx/xxx/"+page;
HttpMethod httpMethod = HttpMethod.GET;
MultiValueMap<String,String> param = new LinkedMultiValueMap<>();
String jsonData = httpClient.client(url,httpMethod,param);

提取数据节点

注:代码块中的三个点...是忽略未展示的代码

通过观察可以发现,我们只需要获取data节点下的数据即可,所以我们将data节点下的数据转换为数组

JSONObject jsonObject = JSONObject.parseObject(jsonData);
JSONArray jsonArray = jsonObject.getJSONArray("data");

对JsonArray数组进行遍历,分割出单条数据

for (int i = 0;i<jsonArray.size();i++){
    String itemData = jsonArray.getString(i);
    JSONObject itemJsonObject = JSONObject.parseObject(itemData);

    String title = itemJsonObject.getString("title");
    String text = itemJsonObject.getString("text");
    String link = itemJsonObject.getString("link");
    ···
}

我们发现,在单条数据下的Author节点下还存在数据,所已我们也要对节点下的数据进行处理,将此节点转换为一个JSONObject对象,通过这个对象我们可以获取数据

String itemAuthorItem = itemJsonObject.getString("author");
JSONObject itemAuthor = JSONObject.parseObject(itemAuthorItem);

String name = itemAuthor.getString("name");
String hash = itemAuthor.getString("hash");
String site = itemAuthor.getString("site");

创建一个时间转换工具类,顺便格式化一下时间戳

public class DateTimeUtils {
 
    public static String convertTimestamp2Date(Long timestamp, String pattern) {
 
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat(pattern);
 
        return simpleDateFormat.format(new Date(timestamp));
    }
}

对时间进行格式化

Long timestamp = Long.parseLong(itemJsonObject.getString("created")) *1000;
String pattern = "yyyy-MM-dd HH:mm:ss";
String created = DateTimeUtils.convertTimestamp2Date(timestamp, pattern);// 调用写好的转换类

创建对象进行数据处理

创建一个DataItem对象来对我们所需要的数据进行处理,并存入一个List中便于前台使用。

package cn.vwmwv.circle.entity;
import lombok.Data;
import java.util.Date;

/**
 * @ClassName: item
 * @Description:
 * @Author: kaygb
 * @Date: 2021/08/2021/8/20 17:12
 */
@Data
public class DataItem {
    String title;
    String text;
    String link;
    String create;
    // 作者信息
    String authorHash;
    String name;
    String site;
}

获取到了信息就可以在for循环中把数据存入List了

List<DataItem> dataItemList = new ArrayList<>();
... 
    for(...){
        DataItem dataItem = new DataItem();
        dataItem.setTitle(title);
        dataItem.setText(text);
        dataItem.setLink(link);
        dataItem.setCreate(created);
        dataItem.setAuthorHash(hash);
        dataItem.setName(name);
        dataItem.setSite(site);
        log.info(title);
        log.info(site);
        dataItemList.add(dataItem);
    }

接下来就是对dataItemList的处理了,可以通过Model交给thymleaf或者其他模板引擎处理。

如果觉得我的文章对你有用,请随意赞赏