It is said that Fastjson is currently the fastest repository for parsing JSON data, and is an open source library developed by people. Top, pay the official website: http://code.alibabatech.com/wiki/pages/viewpage.action?pageId=2424946
To use Fastjson, you first need to download the corresponding jar file, which can be downloaded on the official website.
Attached to the first example of beginners, a lot of advice:
{" statuses": [ { "id": 912345678901, "text": "How does I stream JSON in Java?", "geo": null, " User ": {" name ":" Json_newb ", " Followers_count ":" $ " }, { " id ": 777777777888, " Text ":" Dfngsdnglnsldfnsl ", " geo ": null, " user ": { " name ":" DSFGPD ", " Followers_count ": 24 } } ]}
Allbean's Bean class:
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; } }
Userbean's Bean class:
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.jsonobject;/** * {"statuses": [{"id ": 912345678901," text ":" How does I stream JSON in Java? "," geo ": null," user ": {" name ":" J Son_newb "," Followers_count ": ()}, {" 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 to a JSON object because my JSON data is first the JSON object Jsonobject jobj = Json.parseobject (bean.readrtf (New File ("Json.rtf")); Then there is jsonarray, which can be based on my JSON data to know jsonarray arr = Jobj.getjsonarray ("statuses"); According to the Bean class to each JSON array of items list<allbean> Listbeans = Json.parsearray (arr.tostring (), allbean.class); Traverse for (Allbean Bean_: Listbeans) {//My demo JSON data gets the first layer of data System.out.println (Bean_.gett EXT ()); System.out.println (Bean_.getid ()); My demo JSON data gets the second layer of data System.out.println (Bean_.getuserbean (). Getfollowers_count ()); }}//Because I put the JSON data into the RTF file, which is the JSON data to read the RTF file, converted to the string public string Readrtf (File in) {rtf=new rtfeditork It (); 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, attach the program code: Fastjsontest.zip
From:http://www.cnblogs.com/lee0oo0/archive/2013/05/08/3066371.html
Use of JSON data Fastjson parsing (i)