java USES FastJson to parse Json data

  • 2020-06-07 04:25:44
  • OfStack

fastjson is an excellent JSON parser and generator implemented in Java, developed by engineers from Alibaba.

Main Features:

Fast FAST (faster than any other ES9en-based parser and generator, including jackson) Powerful (supports ordinary JDK classes including any Java Bean Class, Collection, Map, Date, or enum) Zero dependencies (no dependencies on any other libraries except JDK)

1. Generate Json:

JavaBean, List < JavaBean > , List < String > , List < Map < String,Object > >


String jsonString = JSON.toJSONString(obj); 

2. Json:

(1) JavaBean


Class class= JSON.parseObject(jsonString, Class.class); 

(2) List < JavaBean >


List<Class> class=JSON.parseArray((jsonString, Class.class); 

(3) the List < String >


List<String> listString = JSON.parseArray(jsonString, String.class); 

(4) List < Map < String,Object > >


List<Map<String, Object>> listMap = JSON.parseObject(jsonString, new TypeReference<List<Map<String,Object>>>(){}); 

The existing DATA of json:


{"totalRecords":2615, 
"result":{"code":"200","status":"success"}, 
"list":[{"unuAbnId":"0bcd930f-014c-1000-e003-5f160a0d0114", 
"entNo":"1c2e4ca8-00fa-1000-e000-74590a76bf0f", 
"regNO":"442000600169663", 
"entName":"x", 
"entType":"9910 ", 
"speCause":"3", 
"abnTime":"Mar 13, 2015 12:00:00 AM", 
"decOrg":"442020", 
"entNameUrl":"<a href=\".. ", 
"auditingFileNo":"15000684990326", 
"abnormalID":"fd74013d-014b-1000-e00a-72970a0d0114"},{...},{...},...], 
"pageNo":1, 
"pageSize":8, 
"url":"main/abnInfoPage", 
"selList":[{"unuAbnId":"0bcd930f-014c-1000-e003-5f0f0a0d0114", 
"entNo":"16da9629-0131-1000-e005-3effc0a803a8", 
"regNO":"442000602187424", 
"entName":"x", 
"entType":"9910 ", 
"speCause":"3", 
"abnTime":"Mar 13, 2015 12:00:00 AM", 
"decOrg":"442020", 
"entNameUrl":"<a href=\"..\">", 
"auditingFileNo":"15000684990319", 
"abnormalID":"fd74013d-014b-1000-e00a-72970a0d0114"},{...},{...},...], 
"topPageNo":1, 
"totalPages":327, 
"previousPageNo":0, 
"nextPageNo":2, 
"bottomPageNo":327 
} 

list contains 2615 pieces of data and selList contains 8 pieces of data. The goal is to extract the links of entNameUrl in selList (excluding a href=)
The outer layer is JSONObject, the inner list and selList are JSONArrary, and the inner JSONObject. result is also JSONObject


JSONObject jsonObj = JSON.parseObject(rawText); 
JSONArray result = jsonObj.getJSONArray("selList"); 
List<Link> links= JSON.parseArray(result.toJSONString(),Link.class); 

The Link class has the attribute entNameUrl and the setter and getter methods.

In the setter method, you can take a step forward


 public void setEntNameUrl(String entNameUrl) { 
   this.entNameUrl =Html.create(entNameUrl).links().get(); 
} 

The custom method is used here, and its function is to pull the link out of the string.

The Link class can contain attributes such as abnTime, entName, regNO and corresponding getter and setter methods. FastJson can be automatically mapped.

You can also do this by:


JSONObject jsonObj = new JSONObject(rawText); 
JSONArray jsonArray = result .getJSONArray("selList"); 
for (int i = 0; i < jsonArray.length; i++) {   
} 

Related articles: