It is said that fastjson is currently the fastest database for parsing JSON data, and is an open-source library developed by Chinese people. Top, pay on the official website: http://code.alibabatech.com/wiki/pages/viewpage.action? Pageid = 2424946
To use fastjson, first download the corresponding JAR file and download it on the official website.
The first example for beginners is provided:
{ "statuses":[ { "id": 912345678901, "text": "How do I stream JSON in Java?", "geo": null, "user": { "name": "json_newb", "followers_count": 41 } }, { "id": 777777777888, "text": "dfngsdnglnsldfnsl", "geo": null, "user": { "name": "dsfgpd", "followers_count": 24 } } ]}
Bean class of allbean:
package com.lee.JsonToBean;public class AllBean { private long id; private String text; private String geo; private UserBean userBean; public long getId() { return id; } public void setId(long id) { this.id = id; } public String getText() { return text; } public void setText(String text) { this.text = text; } public String getGeo() { return geo; } public void setGeo(String geo) { this.geo = geo; } public UserBean getUserBean() { return userBean; } public void setUserBean(UserBean userBean) { this.userBean = userBean; } }
Bean class of userbean:
package com.lee.JsonToBean;public class UserBean { private String name; private int followers_count; public String getName() { return name; } public void setName(String name) { this.name = name; } public int getFollowers_count() { return followers_count; } public void setFollowers_count(int followers_count) { this.followers_count = followers_count; }}
Parsing class jsonbean:
Package COM. lee. jsontobean; import Java. io. file; import Java. io. fileinputstream; import Java. io. filenotfoundexception; import Java. io. ioexception; import Java. util. list; import javax. swing. text. badlocationexception; import javax. swing. text. defaultstyleddocument; import javax. swing. text. RTF. rtfeditorkit; import COM. alibaba. fastjson. JSON; import COM. alibaba. fastjson. jsonarray; import COM. alibaba. fastjson. jsonob Ject;/*** {"statuses": [{"ID": 912345678901, "text": "How do I stream JSON in Java? "," Geo ": NULL," user ": {" name ":" json_newb "," followers_count ": 41 },{" ID ": 777777777888," text ": "dfngsdnglnsldfnsl", "Geo": NULL, "user": {"name": "dsfgpd", "followers_count ": 24}]} **/public class jsonbean {rtfeditorkit RTF; defaultstyleddocument DSD; string text; public static void main (string [] ARGs) {jsonbean bean = new jsonbean (); // convert the string into a JSON object. This is because my JSON data is first the JSON object jsonobject jobj = JSON. parseobject (bean. readrtf (new file ("JSON. rtf "); // then jsonarray, you can know jsonarray arr = jobj based on my JSON data. getjsonarray ("statuses"); // list the items from the Bean class to each JSON array <allbean> listbeans = JSON. parsearray (ARR. tostring (), allbean. class); // traverse for (allbean bean _: listbeans) {// obtain the first layer of Data System from the JSON data of my demo. out. println (bean _. gettext (); system. out. println (bean _. GETID (); // obtain the second-level data system from the JSON data of this demo. out. println (bean _. getuserbean (). getfollowers_count () ;}// because I put the JSON data into the RTF file, this is to read the JSON data of the RTF file and convert it to the string Public String readrtf (file in) {RTF = new rtfeditorkit (); DSD = new defaultstyleddocument (); try {RTF. read (New fileinputstream (in), DSD, 0); text = new string (DSD. gettext (0, DSD. getlength ();} catch (filenotfoundexception e) {// todo auto-generated Catch Block E. printstacktrace ();} catch (ioexception e) {// todo auto-generated Catch Block E. printstacktrace ();} catch (badlocationexception e) {// todo auto-generated Catch Block E. printstacktrace ();} return text ;}}
Finally, add the program code: fastjsontest.zip.