Home >Java >javaTutorial >java uses FastJson to parse Json data
This article mainly introduces Java's use of FastJson to parse Json data. Fastjson is a JSON parser and generator with excellent performance implemented in Java language. Those who are interested can learn about it.
fastjson is a JSON parser and generator with excellent performance implemented in Java language, developed by engineers from Alibaba.
Main Features:
Fast FAST (faster than any other Java-based parser and generator, including jackson)
Powerful (supports ordinary JDK classes including any Java Bean Class, Collection, Map, Date or enum)
Zero dependency (no dependencies on any other class libraries except JDK)
1. Generate Json:
String jsonString = JSON.toJSONString(obj);##2. Parse Json:
(1)JavaBean
##
Class class= JSON.parseObject(jsonString, Class.class);
(2)List36ee8f7418bd46bbf580ced6560b65cf
List<Class> class=JSON.parseArray((jsonString, Class.class);
(3)Listf7e83be87db5cd2d9a8a0b8117b38cd4
List<String> listString = JSON.parseArray(jsonString, String.class);
(4)Liste7ba61606644f6c6c844a5bf02dab155>
The code is as follows:
List91734776688c4be6c5380beb8ab50658 listMap = JSON.parseObject(jsonString, new TypeReference
{"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 }The list contains 2615 pieces of data, and selList contains 8 pieces of data. The goal is to extract the link of entNameUrl in selList (excluding a href=)
Outer layer It is JSONObject, the list and selList inside are JSONArrary, and the inside is JSONObject. The result is also a JSONObject
JSONObject jsonObj = JSON.parseObject(rawText); JSONArray result = jsonObj.getJSONArray("selList"); List<Link> links= JSON.parseArray(result.toJSONString(),Link.class);
Further processing can be done in the setter method
public void setEntNameUrl(String entNameUrl) { this.entNameUrl =Html.create(entNameUrl).links().get(); }
JSONObject jsonObj = new JSONObject(rawText); JSONArray jsonArray = result .getJSONArray("selList"); for (int i = 0; i < jsonArray.length; i++) { }