我正在用这个 Java 代码抓取一个 web api:
import kong.unirest.HttpResponse;
import kong.unirest.Unirest;
import kong.unirest.json.JSONObject;
public class Scr {
public static void main(String[] args) {
Unirest.config().enableCookieManagement(false);
HttpResponse<String> response = Unirest.post("https://www.cse.lk/api/financials")
.header("content-type", "application/x-www-form-urlencoded; charset=UTF-8")
.body("symbol=DIPD.N0000")
.asString();
JSONObject json = new JSONObject(response.getBody());
//System.out.println(json.toString(4));
}
}
它抛出一个看起来像这样的 json: https ://pastebin.com/ZqqB5H8C
我想弄清楚的是如何将 json 反序列化为 java 对象。这是我为反序列化json而编写的封装bean。
public class ComData {
private List<FileData> infoAnnualData = new ArrayList<FileData>();
private List<FileData> infoQuarterlyData = new ArrayList<FileData>();
private List<FileData> infoOtherData = new ArrayList<FileData>();
private List<InfoWebLink> infoWebLink = new ArrayList<InfoWebLink>();
private List<ReqFinancial> reqFinancial = new ArrayList<ReqFinancial>();
private InfoCompanyBannerAd infoCompanyBannerAd;
// getters and setters goes here
}