电竞英雄联盟数据API接口 - 【比赛列表】API调用示例代码

电竞英雄联盟数据API接口 - 【比赛列表】API调用示例代码

分享使用 野子科技数据 https://www.xxe.io/ 接口调用的示例代码,今天接的是英雄联盟的【比赛列表】接口,跟之前不同的是,接口返回的是Json数据,这里使用Fastjson来解析。

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.annotation.JSONField;

import java.nio.charset.StandardCharsets;
import java.nio.file.Files;https://mp.csdn.net/mdeditor/99860909
import java.nio.file.Paths;
import java.util.List;

/**

  • @API: 联赛列表

  • @Website: https://www.xxe.io/
    */
    public class LolLeague {

    public static void main(String[] args) {
    try {
    String content = getContent();
    Respond rsp = JSON.parseObject(content, Respond.class);
    System.out.println(rsp.code);
    System.out.println(rsp.message);
    rsp.getLeagueList().forEach(System.out::println);

     } catch (Throwable t) {
         t.printStackTrace();
     }
    

    }

    /**

    • 获取API返回内容
    • Note: 这里为了方便测试我使用了一份本地文件,使用时应替换为真实接口返回内容
      */
      private static String getContent() {
      try {
      StringBuilder builder = new StringBuilder();
      List lines = Files.readAllLines(Paths.get("./src/main/resources/LolLeague.json"), StandardCharsets.UTF_8);
      lines.forEach(builder::append);
      return builder.toString();
      } catch (Throwable t) {
      t.printStackTrace();
      return “”;
      }
      }

    public static class Respond {
    @JSONField(name = “code”)
    private int code;
    @JSONField(name = “message”)
    private String message;
    @JSONField(name = “data”)
    private List leagueList;

     public int getCode() {
         return code;
     }
    
     public void setCode(int code) {
         this.code = code;
     }
    
     public String getMessage() {
         return message;
     }
    
     public void setMessage(String message) {
         this.message = message;
     }
    
     public List getLeagueList() {
         return leagueList;
     }
    
     public void setLeagueList(List leagueList) {
         this.leagueList = leagueList;
     }
    

    }

    public static class League {
    @JSONField(name = “league_id”)
    private int leagueId;
    @JSONField(name = “name”)
    private String name;
    @JSONField(name = “name_en”)
    private String nameEn;
    @JSONField(name = “short_name”)
    private String nameShort;
    @JSONField(name = “start_time”)
    private long startTime;
    @JSONField(name = “end_time”)
    private long endTime;
    @JSONField(name = “organizer”)
    private String organizer;
    @JSONField(name = “logo”)
    private String logo;
    @JSONField(name = “address”)
    private String address;
    @JSONField(name = “team_ids”)
    private List teamIds;
    @JSONField(name = “status”)
    private int status;

     @Override
     public String toString() {
         return "League{" +
                 "leagueId=" + leagueId +
                 ", name='" + name + '\'' +
                 ", nameEn='" + nameEn + '\'' +
                 ", nameShort='" + nameShort + '\'' +
                 ", startTime=" + startTime +
                 ", endTime=" + endTime +
                 ", organizer='" + organizer + '\'' +
                 ", logo='" + logo + '\'' +
                 ", address='" + address + '\'' +
                 ", teamIds=" + teamIds +
                 ", status=" + status +
                 '}';
     }
    
     public void setLeagueId(int leagueId) {
         this.leagueId = leagueId;
     }
    
     public void setName(String name) {
         this.name = name;
     }
    
     public void setNameEn(String nameEn) {
         this.nameEn = nameEn;
     }
    
     public void setNameShort(String nameShort) {
         this.nameShort = nameShort;
     }
    
     public void setStartTime(long startTime) {
         this.startTime = startTime;
     }
    
     public void setEndTime(long endTime) {
         this.endTime = endTime;
     }
    
     public void setOrganizer(String organizer) {
         this.organizer = organizer;
     }
    
     public void setLogo(String logo) {
         this.logo = logo;
     }
    
     public void setAddress(String address) {
         this.address = address;
     }
    
     public void setTeamIds(List teamIds) {
         this.teamIds = teamIds;
     }
    
     public void setStatus(int status) {
         this.status = status;
     }
    

    }
    }

你可能感兴趣的:(电竞英雄联盟数据API接口 - 【比赛列表】API调用示例代码)