Home  >  Article  >  Java  >  java uses FastJson to parse Json data

java uses FastJson to parse Json data

高洛峰
高洛峰Original
2017-02-11 16:04:182026browse

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:

##JavaBean, List36ee8f7418bd46bbf580ced6560b65cf, Listf7e83be87db5cd2d9a8a0b8117b38cd4, Lista06c5c2dfccfdea21bf0b155d9343663>


##
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>


##Copy code

The code is as follows:

List91734776688c4be6c5380beb8ab50658 listMap = JSON.parseObject(jsonString, new TypeReference>(){});

Existing json data like this:



{"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);


The Link class must have the attribute entNameUrl, as well as setter and getter methods.

Further processing can be done in the setter method


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


A custom method is used here, and its function is to take out Link in string.

The Link class can contain abnTime, entName, regNO and other attributes and corresponding getter and setter methods, and FastJson can automatically map them.

It can also be processed through the following methods:

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


The above is the entire content of this article, I hope it will be helpful to everyone’s learning Help, and I hope everyone will support the PHP Chinese website.

For more articles related to Java using FastJson to parse Json data, please pay attention to the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn