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

java uses FastJson to parse Json data

高洛峰
Release: 2017-02-11 16:04:18
Original
2092 people have browsed it

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, List, List, List>


##
String jsonString = JSON.toJSONString(obj);
Copy after login

##2. Parse Json:


(1)JavaBean

##

Class class= JSON.parseObject(jsonString, Class.class);
Copy after login

(2)List

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

(3)List

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

(4)List>


##Copy code

The code is as follows:

List 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 
}
Copy after login

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);
Copy after login


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(); 
}
Copy after login


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++) {   
}
Copy after login


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!

Related labels:
source:php.cn
Statement of this Website
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template