Use fastjson to parse JSON data (1)

Source: Internet
Author: User

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.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.