什么是fastjson
fastjson是阿里巴巴的开源JSON解析库,它可以解析JSON格式的字符串,支持将Java Bean序列化为JSON字符串,也可以从JSON字符串反序列化到JavaBean
fastjson配置Maven依赖
<!-- https://mvnrepository.com/artifact/com.alibaba/fastjson -->
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>fastjson</artifactId>
<version>1.2.79</version>
</dependency>
方法类转换图如下:
对象关系:
JSON.toJSONString(Object javaObject)
JSON.toJavaObject(JSON json,Class clazz)
JSON.parseObject(String text)
JSON.parseObject(String text,Class clazz)
JSON.toJSONString(Object javaObject)
(JSONObject)JSON.toJSON(Object javaObject)
JSON对象
JSON字符串
Java对象
数组集合关系:
JSON.toJSONString(Object object)
不能直接转:先转字符串再转集合
JSON.parseArray(String text)
JSON.parseArray(String text,Class clazz)
JSON.toJSONString(Object object)
(JSONArray)JSON.toJSON(Object javaObject)
JSON数组
JSON字符串
Java集合
总图:
JSON.toJSONString(Object object)
间接转:数组->字符串->集合
对象:JSON.parseObject(String text,Class clazz)
集合:JSON.parseArray(String text,Class clazz)对象:JSON.parseObject(String text)
数组:JSON.parseArray(String text)JSON.toJSONString(Object object)
对象:(JSONObject)JSON.toJSON(Object javaObject)
集合:(JSONArray)JSON.toJSON(Object javaObject)
JSON对象(数组)
JSON字符串
Java对象(集合)
以中国外汇交易中心源码为例:
添加HttpClient获取其网页源码:
数据采集所需依赖有:
<dependency>
<groupId>org.apache.httpcomponents</groupId>
<artifactId>httpclient</artifactId>
<version>4.5.6</version>
</dependency>
先创建几个实体类(实体类的变量名应与解析的JSON字符串中变量名相同):
创建实体类Page.java
:
public class Page {
private String head;
private String data;
private String records;
public String getHead() {
return head;
}
public void setHead(String head) {
this.head = head;
}
public String getData() {
return data;
}
public void setData(String data) {
this.data = data;
}
public String getRecords() {
return records;
}
public void setRecords(String records) {
this.records = records;
}
}
创建实体类Head.java
:
public class Head {
private String version;
private String provider;
private String req_code;
private String rep_code;
private String rep_message;
private String ts;
private String producer;
public String getVersion() {
return version;
}
public void setVersion(String version) {
this.version = version;
}
public String getProvider() {
return provider;
}
public void setProvider(String provider) {
this.provider = provider;
}
public String getReq_code() {
return req_code;
}
public void setReq_code(String req_code) {
this.req_code = req_code;
}
public String getRep_code() {
return rep_code;
}
public void setRep_code(String rep_code) {
this.rep_code = rep_code;
}
public String getRep_message() {
return rep_message;
}
public void setRep_message(String rep_message) {
this.rep_message = rep_message;
}
public String getTs() {
return ts;
}
public void setTs(String ts) {
this.ts = ts;
}
public String getProducer() {
return producer;
}
public void setProducer(String producer) {
this.producer = producer;
}
}
创建实体类Record.java
:
public class Record {
private String vrtCode;
private String price;
private String bp;
private String vrtName;
private String vrtEName;
private String foreignCName;
private String bpDouble;
private String approvedTime;
private String approvedTimeEn;
private String showDate;
private String showDateForCn;
private String lastMonthAvgPrice;
private String monthPrice;
private String quarterPrice;
private String yearPrice;
private String isShowBp;
private String show;
private String url;
private String bannerPic;
private String bannerCss;
public String getVrtCode() {
return vrtCode;
}
public void setVrtCode(String vrtCode) {
this.vrtCode = vrtCode;
}
public String getPrice() {
return price;
}
public void setPrice(String price) {
this.price = price;
}
public String getBp() {
return bp;
}
public void setBp(String bp) {
this.bp = bp;
}
public String getVrtName() {
return vrtName;
}
public void setVrtName(String vrtName) {
this.vrtName = vrtName;
}
public String getVrtEName() {
return vrtEName;
}
public void setVrtEName(String vrtEName) {
this.vrtEName = vrtEName;
}
public String getForeignCName() {
return foreignCName;
}
public void setForeignCName(String foreignCName) {
this.foreignCName = foreignCName;
}
public String getBpDouble() {
return bpDouble;
}
public void setBpDouble(String bpDouble) {
this.bpDouble = bpDouble;
}
public String getApprovedTime() {
return approvedTime;
}
public void setApprovedTime(String approvedTime) {
this.approvedTime = approvedTime;
}
public String getApprovedTimeEn() {
return approvedTimeEn;
}
public void setApprovedTimeEn(String approvedTimeEn) {
this.approvedTimeEn = approvedTimeEn;
}
public String getShowDate() {
return showDate;
}
public void setShowDate(String showDate) {
this.showDate = showDate;
}
public String getShowDateForCn() {
return showDateForCn;
}
public void setShowDateForCn(String showDateForCn) {
this.showDateForCn = showDateForCn;
}
public String getLastMonthAvgPrice() {
return lastMonthAvgPrice;
}
public void setLastMonthAvgPrice(String lastMonthAvgPrice) {
this.lastMonthAvgPrice = lastMonthAvgPrice;
}
public String getMonthPrice() {
return monthPrice;
}
public void setMonthPrice(String monthPrice) {
this.monthPrice = monthPrice;
}
public String getQuarterPrice() {
return quarterPrice;
}
public void setQuarterPrice(String quarterPrice) {
this.quarterPrice = quarterPrice;
}
public String getYearPrice() {
return yearPrice;
}
public void setYearPrice(String yearPrice) {
this.yearPrice = yearPrice;
}
public String getIsShowBp() {
return isShowBp;
}
public void setIsShowBp(String isShowBp) {
this.isShowBp = isShowBp;
}
public String getShow() {
return show;
}
public void setShow(String show) {
this.show = show;
}
public String getUrl() {
return url;
}
public void setUrl(String url) {
this.url = url;
}
public String getBannerPic() {
return bannerPic;
}
public void setBannerPic(String bannerPic) {
this.bannerPic = bannerPic;
}
public String getBannerCss() {
return bannerCss;
}
public void setBannerCss(String bannerCss) {
this.bannerCss = bannerCss;
}
}
从中国外汇交易中心获取json中的数据程序,如下:
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.util.EntityUtils;
import java.util.ArrayList;
import java.util.List;
public class Main {
public static void main(String[] args) {
String text="";
try
{
/* 以GET方法获取中国外汇交易中心网页的json数据 */
CloseableHttpClient httpClient=new DefaultHttpClient();
HttpGet httpGet=new HttpGet("http://www.chinamoney.com.cn/r/cms/www/chinamoney/data/fx/ccpr.json");
HttpResponse response=httpClient.execute(httpGet);
HttpEntity entity= response.getEntity();
if(entity!=null)
{
text= EntityUtils.toString(entity,"UTF-8");
EntityUtils.consume(entity);
}
/* 调用fastjson解析JSON字符串 */
// JSON字符串-->JSON对象
JSONObject jsonObject= JSON.parseObject(text);
Page page=JSON.toJavaObject(jsonObject,Page.class);
System.out.println("Page.Head="+page.getHead());
System.out.println("Page.Data="+page.getData());
System.out.println("Page.Records="+page.getRecords());
// JSON对象-->JSON数组-->JSON字符串-->Java集合
JSONArray jsonArray=jsonObject.getJSONArray("records");
List<Record> records=new ArrayList<>();
for(int i=0;i<jsonArray.size();i++)
{
Record record=JSON.parseObject(jsonArray.getString(i),Record.class);
records.add(record);
}
for(Record record:records)
{
System.out.println("record记录有:");
System.out.println("vrtCode="+record.getVrtCode());
System.out.println("price="+record.getPrice());
System.out.println("bp="+record.getBp());
System.out.println("vrtName="+record.getVrtName());
System.out.println("vrtEName="+record.getVrtEName());
System.out.println("foreignCName="+record.getForeignCName());
System.out.println("bpDouble="+record.getBpDouble());
System.out.println("approvedTime="+record.getApprovedTime());
System.out.println("approvedTimeEn="+record.getApprovedTimeEn());
System.out.println("showDate="+record.getShowDate());
System.out.println("showDateForCn="+record.getShowDateForCn());
System.out.println("lastMonthAvgPrice="+record.getLastMonthAvgPrice());
System.out.println("monthPrice="+record.getMonthPrice());
System.out.println("quarterPrice="+record.getQuarterPrice());
System.out.println("yearPrice="+record.getYearPrice());
System.out.println("isShowBp="+record.getIsShowBp());
System.out.println("show="+record.getShow());
System.out.println("url="+record.getUrl());
System.out.println("bannerPic="+record.getBannerPic());
System.out.println("bannerCss="+record.getBannerCss());
System.out.println();
}
// JSON字符串-->JSON对象--->JSON字符串--->Java对象
String headtext=jsonObject.getString("head");
Head head=JSON.parseObject(headtext,Head.class);
System.out.println("Head.Version="+head.getVersion());
System.out.println("Head.provider="+head.getProvider());
System.out.println("Head.req_code="+head.getReq_code());
System.out.println("Head.rep_code="+head.getRep_code());
System.out.println("Head.rep_message="+head.getRep_message());
System.out.println("Head.ts="+head.getTs());
System.out.println("Head.producer="+head.getProducer());
}
catch (Exception e)
{
e.printStackTrace();
}
}
}
注:为防止原网站升级迁移,故附源码于文后:
网页json源码:
{"head":{"version":"2.0","provider":"CWAP","req_code":"","rep_code":"200","rep_message":"","ts":1648430740382,"producer":""},"data":{"lastDateEn":"28/03/2022 9:15","lastDate":"2022-03-28 9:15","pairChange":"美元/人民币欧元/人民币100日元/人民币港元/人民币英镑/人民币澳元/人民币新西兰元/人民币新加坡元/人民币瑞士法郎/人民币加元/人民币人民币/马来西亚林吉特人民币/俄罗斯卢布人民币/南非兰特人民币/韩元人民币/阿联酋迪拉姆人民币/沙特里亚尔人民币/匈牙利福林人民币/波兰兹罗提人民币/丹麦克朗人民币/瑞典克朗人民币/挪威克朗人民币/土耳其里拉人民币/墨西哥比索人民币/泰铢"},"records":[{"vrtCode":"1","price":"6.3732","bp":"7.00","vrtName":"美元/人民币","vrtEName":"USD/CNY","foreignCName":"USD","bpDouble":-7.0,"approvedTime":"","approvedTimeEn":"","showDate":"","showDateForCn":"","lastMonthAvgPrice":"---","monthPrice":"---","quarterPrice":"---","yearPrice":"---","isShowBp":true,"show":true,"url":"","bannerPic":"/r/cms/www/chinamoney/assets/images/flag/flag_usd.gif","bannerCss":""},{"vrtCode":"2","price":"6.9993","bp":"162.00","vrtName":"欧元/人民币","vrtEName":"EUR/CNY","foreignCName":"EUR","bpDouble":-162.0,"approvedTime":"","approvedTimeEn":"","showDate":"","showDateForCn":"","lastMonthAvgPrice":"---","monthPrice":"---","quarterPrice":"---","yearPrice":"---","isShowBp":true,"show":true,"url":"","bannerPic":"/r/cms/www/chinamoney/assets/images/flag/20160912152157516.jpg","bannerCss":""},{"vrtCode":"3","price":"5.2138","bp":"34.00","vrtName":"100日元/人民币","vrtEName":"100JPY/CNY","foreignCName":"JPY","bpDouble":34.0,"approvedTime":"","approvedTimeEn":"","showDate":"","showDateForCn":"","lastMonthAvgPrice":"---","monthPrice":"---","quarterPrice":"---","yearPrice":"---","isShowBp":true,"show":true,"url":"","bannerPic":"/r/cms/www/chinamoney/assets/images/flag/20160912152213196.jpg","bannerCss":""},{"vrtCode":"4","price":"0.81407","bp":"6.50","vrtName":"港元/人民币","vrtEName":"HKD/CNY","foreignCName":"HKD","bpDouble":-6.5,"approvedTime":"","approvedTimeEn":"","showDate":"","showDateForCn":"","lastMonthAvgPrice":"---","monthPrice":"---","quarterPrice":"---","yearPrice":"---","isShowBp":true,"show":true,"url":"","bannerPic":"/r/cms/www/chinamoney/assets/images/flag/20160912151836268.jpg","bannerCss":""},{"vrtCode":"5","price":"8.3942","bp":"161.00","vrtName":"英镑/人民币","vrtEName":"GBP/CNY","foreignCName":"GBP","bpDouble":-161.0,"approvedTime":"","approvedTimeEn":"","showDate":"","showDateForCn":"","lastMonthAvgPrice":"---","monthPrice":"---","quarterPrice":"---","yearPrice":"---","isShowBp":true,"show":true,"url":"","bannerPic":"/r/cms/www/chinamoney/assets/images/flag/20160912152141840.jpg","bannerCss":""},{"vrtCode":"6","price":"4.7912","bp":"36.00","vrtName":"澳元/人民币","vrtEName":"AUD/CNY","foreignCName":"AUD","bpDouble":36.0,"approvedTime":"","approvedTimeEn":"","showDate":"","showDateForCn":"","lastMonthAvgPrice":"---","monthPrice":"---","quarterPrice":"---","yearPrice":"---","isShowBp":true,"show":true,"url":"","bannerPic":"/r/cms/www/chinamoney/assets/images/flag/20160912151915601.jpg","bannerCss":""},{"vrtCode":"7","price":"4.4342","bp":"25.00","vrtName":"新西兰元/人民币","vrtEName":"NZD/CNY","foreignCName":"NZD","bpDouble":-25.0,"approvedTime":"","approvedTimeEn":"","showDate":"","showDateForCn":"","lastMonthAvgPrice":"---","monthPrice":"---","quarterPrice":"---","yearPrice":"---","isShowBp":true,"show":true,"url":"","bannerPic":"/r/cms/www/chinamoney/assets/images/flag/20160912151950699.jpg","bannerCss":""},{"vrtCode":"8","price":"4.6911","bp":"34.00","vrtName":"新加坡元/人民币","vrtEName":"SGD/CNY","foreignCName":"SGD","bpDouble":-34.0,"approvedTime":"","approvedTimeEn":"","showDate":"","showDateForCn":"","lastMonthAvgPrice":"---","monthPrice":"---","quarterPrice":"---","yearPrice":"---","isShowBp":true,"show":true,"url":"","bannerPic":"/r/cms/www/chinamoney/assets/images/flag/20160912152004500.jpg","bannerCss":""},{"vrtCode":"12","price":"6.8446","bp":"144.00","vrtName":"瑞士法郎/人民币","vrtEName":"CHF/CNY","foreignCName":"CHF","bpDouble":-144.0,"approvedTime":"","approvedTimeEn":"","showDate":"","showDateForCn":"","lastMonthAvgPrice":"---","monthPrice":"---","quarterPrice":"---","yearPrice":"---","isShowBp":true,"show":true,"url":"","bannerPic":"/r/cms/www/chinamoney/assets/images/flag/20160912152058150.jpg","bannerCss":""},{"vrtCode":"9","price":"5.1059","bp":"174.00","vrtName":"加元/人民币","vrtEName":"CAD/CNY","foreignCName":"CAD","bpDouble":174.0,"approvedTime":"","approvedTimeEn":"","showDate":"","showDateForCn":"","lastMonthAvgPrice":"---","monthPrice":"---","quarterPrice":"---","yearPrice":"---","isShowBp":true,"show":true,"url":"","bannerPic":"/r/cms/www/chinamoney/assets/images/flag/20160912152019513.jpg","bannerCss":""},{"vrtCode":"10","price":"0.66052","bp":"23.80","vrtName":"人民币/马来西亚林吉特","vrtEName":"CNY/MYR","foreignCName":"MYR","bpDouble":-23.8,"approvedTime":"","approvedTimeEn":"","showDate":"","showDateForCn":"","lastMonthAvgPrice":"---","monthPrice":"---","quarterPrice":"---","yearPrice":"---","isShowBp":true,"show":true,"url":"","bannerPic":"/r/cms/www/chinamoney/assets/images/flag/20160912152030828.jpg","bannerCss":""},{"vrtCode":"11","price":"15.7002","bp":"3332.00","vrtName":"人民币/俄罗斯卢布","vrtEName":"CNY/RUB","foreignCName":"RUB","bpDouble":-3332.0,"approvedTime":"","approvedTimeEn":"","showDate":"","showDateForCn":"","lastMonthAvgPrice":"---","monthPrice":"---","quarterPrice":"---","yearPrice":"---","isShowBp":true,"show":true,"url":"","bannerPic":"/r/cms/www/chinamoney/assets/images/flag/20160912152045202.jpg","bannerCss":""},{"vrtCode":"41","price":"2.2839","bp":"60.00","vrtName":"人民币/南非兰特","vrtEName":"CNY/ZAR","foreignCName":"ZAR","bpDouble":60.0,"approvedTime":"","approvedTimeEn":"","showDate":"","showDateForCn":"","lastMonthAvgPrice":"---","monthPrice":"---","quarterPrice":"---","yearPrice":"---","isShowBp":true,"show":true,"url":"","bannerPic":"/r/cms/www/chinamoney/assets/images/flag/20160912152118608.jpg","bannerCss":""},{"vrtCode":"42","price":"191.87","bp":"49.00","vrtName":"人民币/韩元","vrtEName":"CNY/KRW","foreignCName":"KRW","bpDouble":49.0,"approvedTime":"","approvedTimeEn":"","showDate":"","showDateForCn":"","lastMonthAvgPrice":"---","monthPrice":"---","quarterPrice":"---","yearPrice":"---","isShowBp":true,"show":true,"url":"","bannerPic":"/r/cms/www/chinamoney/assets/images/flag/20160912152133696.jpg","bannerCss":""},{"vrtCode":"61","price":"0.57617","bp":"0.40","vrtName":"人民币/阿联酋迪拉姆","vrtEName":"CNY/AED","foreignCName":"AED","bpDouble":-0.4,"approvedTime":"","approvedTimeEn":"","showDate":"","showDateForCn":"","lastMonthAvgPrice":"---","monthPrice":"---","quarterPrice":"---","yearPrice":"---","isShowBp":true,"show":true,"url":"","bannerPic":"/r/cms/www/chinamoney/assets/images/flag/20160923172345816.jpg","bannerCss":""},{"vrtCode":"62","price":"0.58844","bp":"0.40","vrtName":"人民币/沙特里亚尔","vrtEName":"CNY/SAR","foreignCName":"SAR","bpDouble":-0.4,"approvedTime":"","approvedTimeEn":"","showDate":"","showDateForCn":"","lastMonthAvgPrice":"---","monthPrice":"---","quarterPrice":"---","yearPrice":"---","isShowBp":true,"show":true,"url":"","bannerPic":"/r/cms/www/chinamoney/assets/images/flag/20160923172544634.jpg","bannerCss":""},{"vrtCode":"63","price":"53.2125","bp":"2547.00","vrtName":"人民币/匈牙利福林","vrtEName":"CNY/HUF","foreignCName":"HUF","bpDouble":-2547.0,"approvedTime":"","approvedTimeEn":"","showDate":"","showDateForCn":"","lastMonthAvgPrice":"---","monthPrice":"---","quarterPrice":"---","yearPrice":"---","isShowBp":true,"show":true,"url":"","bannerPic":"/r/cms/www/chinamoney/assets/images/flag/20161209185627409.jpg","bannerCss":""},{"vrtCode":"64","price":"0.66953","bp":"103.70","vrtName":"人民币/波兰兹罗提","vrtEName":"CNY/PLN","foreignCName":"PLN","bpDouble":-103.7,"approvedTime":"","approvedTimeEn":"","showDate":"","showDateForCn":"","lastMonthAvgPrice":"---","monthPrice":"---","quarterPrice":"---","yearPrice":"---","isShowBp":true,"show":true,"url":"","bannerPic":"/r/cms/www/chinamoney/assets/images/flag/20161209185813438.jpg","bannerCss":""},{"vrtCode":"65","price":"1.0627","bp":"22.00","vrtName":"人民币/丹麦克朗","vrtEName":"CNY/DKK","foreignCName":"DKK","bpDouble":22.0,"approvedTime":"","approvedTimeEn":"","showDate":"","showDateForCn":"","lastMonthAvgPrice":"---","monthPrice":"---","quarterPrice":"---","yearPrice":"---","isShowBp":true,"show":true,"url":"","bannerPic":"/r/cms/www/chinamoney/assets/images/flag/20161209185905504.jpg","bannerCss":""},{"vrtCode":"66","price":"1.4776","bp":"23.00","vrtName":"人民币/瑞典克朗","vrtEName":"CNY/SEK","foreignCName":"SEK","bpDouble":23.0,"approvedTime":"","approvedTimeEn":"","showDate":"","showDateForCn":"","lastMonthAvgPrice":"---","monthPrice":"---","quarterPrice":"---","yearPrice":"---","isShowBp":true,"show":true,"url":"","bannerPic":"/r/cms/www/chinamoney/assets/images/flag/20161209190017458.jpg","bannerCss":""},{"vrtCode":"67","price":"1.3532","bp":"60.00","vrtName":"人民币/挪威克朗","vrtEName":"CNY/NOK","foreignCName":"NOK","bpDouble":-60.0,"approvedTime":"","approvedTimeEn":"","showDate":"","showDateForCn":"","lastMonthAvgPrice":"---","monthPrice":"---","quarterPrice":"---","yearPrice":"---","isShowBp":true,"show":true,"url":"","bannerPic":"/r/cms/www/chinamoney/assets/images/flag/20161209190108743.jpg","bannerCss":""},{"vrtCode":"68","price":"2.32748","bp":"15.80","vrtName":"人民币/土耳其里拉","vrtEName":"CNY/TRY","foreignCName":"TRY","bpDouble":15.8,"approvedTime":"","approvedTimeEn":"","showDate":"","showDateForCn":"","lastMonthAvgPrice":"---","monthPrice":"---","quarterPrice":"---","yearPrice":"---","isShowBp":true,"show":true,"url":"","bannerPic":"/r/cms/www/chinamoney/assets/images/flag/20161209190206460.jpg","bannerCss":""},{"vrtCode":"69","price":"3.1430","bp":"80.00","vrtName":"人民币/墨西哥比索","vrtEName":"CNY/MXN","foreignCName":"MXN","bpDouble":-80.0,"approvedTime":"","approvedTimeEn":"","showDate":"","showDateForCn":"","lastMonthAvgPrice":"---","monthPrice":"---","quarterPrice":"---","yearPrice":"---","isShowBp":true,"show":true,"url":"","bannerPic":"/r/cms/www/chinamoney/assets/images/flag/20161209190255587.jpg","bannerCss":""},{"vrtCode":"13","price":"5.2740","bp":"127.00","vrtName":"人民币/泰铢","vrtEName":"CNY/THB","foreignCName":"THB","bpDouble":127.0,"approvedTime":"","approvedTimeEn":"","showDate":"","showDateForCn":"","lastMonthAvgPrice":"---","monthPrice":"---","quarterPrice":"---","yearPrice":"---","isShowBp":true,"show":true,"url":"","bannerPic":"/r/cms/www/chinamoney/assets/images/flag/20180203155245539.jpg","bannerCss":""}]}