The use of Fast-json.jar

Source: Internet
Author: User

Fast-json.jar parsing JSON data: One way to parse JSON data is this, click here to download the JSONFAST.JAR+FASTJSONAPI document

[{"id": 6378, "title": "Test", "img": "Http://image.jxvdy.com/2014/0929/5428d91c9e6dc8f78fd99_0". PNG "," score ": 0," description ":" Test "," Time ": 1411963174}, {" id ": 6142," tit        Le ":" Micro-film multi-angle shooting Skills (iii) "," img ":" Http://image.jxvdy.com/old/201409/24/11-54-15-17-1531.jpg "," score ": 0,  "description": "", "Time": 1411530850}, {"id": 6141, "title": "Micro-movie multi-angle shooting skills (i)", "IMG": "Http://image.jxvdy.com/old/201409/24/11-54-04-89-1531.jpg", "score": 0, "description": "", "Time": 1411530835}, {"id": 6140, "title": "Micro-movie multi-angle shooting skills (ii)", "img": "http://image.jxvdy.com/old/201409 /24/11-49-54-18-1531.jpg "," score ": 0," description ":" "," Time ": 1411530552}, {" id ": 4355, "title": "Bless, More Blessed", "img": "Http://image.jxvdy.com/old/201409/24/11-46-06-65-3.jpg", "score": 0 , "description":"An old man uses his half time to help others, to give help, to reap happiness", "Time": 1411530082}, {"id": 4354, "title": "Father and Son Journey", "I MG ":" Http://image.jxvdy.com/old/201409/24/11-35-13-81-3.jpg "," score ": 0," description ":" When the father is old, the man who is busy living has no time Taking care of the weak father, so, take his father on the road, and take him back to the time he walked through the journey of countless times.

For this kind of JSON data, when parsing using Fastjson, you should write out its corresponding Bean.java before calling the method (which I think you have already done); the corresponding bean for the JSON data above is this,

public class Newmoviesbean {private int id;private string Title;private string Img;private string Score;private string des cription;private int time;public int getId () {return ID;} public void setId (int id) {this.id = ID;} Public String GetTitle () {return title;} public void Settitle (String title) {this.title = title;} Public String getimg () {return img;} public void Setimg (String img) {this.img = img;} Public String Getscore () {return score;} public void SetScore (String score) {This.score = score;} Public String GetDescription () {return description;} public void SetDescription (String description) {this.description = description;} public int GetTime () {return time;} public void settime (int time) {this.time = time;} public Newmoviesbean (int ID, string title, String img, string score,string description, int time) {super (); this.id = id;th Is.title = title;this.img = Img;this.score = Score;this.description = Description;this.time = time;} Public Newmoviesbean () {super ();} @Overridepublic String toString () {return "Newmoviesbean [id=" + ID + ", title=" + title + ", img=" + img+ ", score=" + score + ", description=" + descript ion+ ", time=" + Time + "]";}}


Then the corresponding parsing method is this:

Json.parsearray (JSON, newmoviesbean.class);
Why is there such an analytic approach? Because, parsing the entire JSON data format we can find that the outermost is the middle bracket "[]", the inner side is the curly brace "{}", the square brackets indicate that the entire JSON data is an array type, in which the curly brace description is an element in the array; it means that the whole is a jsonarray, The elements in Jsonarray are also jsonobject.

There is also a way of parsing: JSON data is this,

{"Type": [        "Horror",        "Plot"    ]},

Parsing such a form, the curly braces are enclosed in parentheses. That is, the whole array is presented in the form of key-value pairs. Then the outermost layer is a jsonobject,key corresponding to the Jsonarray. This should be the case:

Jsonarray Jsonarraytype = Jsonobject.getjsonarray ("type"); string[] Type = new String[jsonarraytype.size ()];for (int j = 0; J < Jsonarraytype.size (); j + +) {Type[j] = (String) JSON Arraytype.get (j);}

This will parse out the desired data.

Similar to the above, there is also a parsing: JSON data is this:

{"        Playurl": {            "360P": "Http://v.jxvdy.com/sendfile/V7bzjsH5sIZlBzVG7t7qbL1u-y1_ K6e0dctzyz8iv-prf3gmewwoj-hq_grnppgnnx_rrhb-bztnwavzgq ",            " 480P ":" http://v.jxvdy.com/sendfile/ V7bzjsh5sizlbzvg7t7qbl1u-y1_k6e0dctzyz8iv-prf3gmewwoj-hq_grnppgnnx_rrhb-bztnwavzgt ",            " 720P ":"/HTTP// V.jxvdy.com/sendfile/v7bzjsh5sizlbzvg7t7qbl1u-y1_k6e0dctzyz8iv-prf3gmewwoj-hq_grnppgnnx_rrhb-bztnwavzgz "        }    }


In this form, the outer brace is a key, and the enclosing element is a brace, and the outermost layer is a jsonobject, and the inner key corresponds to a jsonobject.

Of course, you can create a bean first:

public class Moviedefinitionbean {private String normalp;private string hightp;private string Superp;public string Getnor Malp () {return normalp;} public void Setnormalp (String normalp) {This.normalp = Normalp;} Public String GETHIGHTP () {return HIGHTP;} public void Sethightp (String hightp) {THIS.HIGHTP = HIGHTP;} Public String Getsuperp () {return superp;} public void Setsuperp (String superp) {this.superp = Superp;} Public Moviedefinitionbean (String Normalp, String hightp, String superp) {super (); This.normalp = NORMALP;THIS.HIGHTP = Hi Ghtp;this.superp = Superp;} Public Moviedefinitionbean () {super ();} @Overridepublic String toString () {return "Moviedefinitionbean [normalp=" + Normalp + ", hightp=" + hightp+ ", superp=" + Superp + "]";}}


This is then parsed:

Jsonobject jsonobjectdefination = Jsonobject.getjsonobject ("Playurl"); String Normalp = jsonobjectdefination.getstring ("360P"); String HIGHTP = jsonobjectdefination.getstring ("480P"); String superp = jsonobjectdefination.getstring ("720P");p Layurl = new Moviedefinitionbean (Normalp, HIGHTP, SUPERP);

Let's write this |10-02-2014 here today.

I'm going to write today. There is another type of parsing: give the JSON data first:

{    "1": "Love", "2": "Youth", "3": "Warmth", "4": "Comedy", "5": "Suspense", "6": "Inspirational", "7": "    Workplace", "    8": " Society ",    " 9 ":" Criminal Investigation ",    " 10 ":" War ","    11 ":" Costume ",    " 12 ":" Sci-fi ","    13 ":" Action "," 14 ":"    Crossing ",    " 15 ":" Advertisement ",    " 16 ":" Public Welfare ","    17 ":" Horror ","    18 ":" Literature ",    " 19 ":" Record ","    20 ":" Animation ",    " 21 ":" Plot ",    " 22 " : "Other"}


Then we can see that the JSON data above is a jsonobject, and he is also a type of map-like data that exists in the form of key-value pairs. So that means you can use the traversal map to parse the josnobject.

public static void Jsonparsefilterfields (String json) {ids = new linkedlist<integer> (); items = new linkedlist< String> (); Jsonobject Jsonobject = Jsonobject.parseobject (JSON);iterator<string> it = Jsonobject.keyset (). Iterator (); while (It.hasnext ()) {String key = It.next (); Ids.add (Integer.valueof (key)); Items.Add (Jsonobject.getstring (key));}

To be continued; 2014-10-4
Today there is a high-speed approach to parsing: JSON data is this

{    introduce= Piglet's growth course,    face=http://www.bejson.com/imgsss,    sex=,    nick= Pig,    [email protected]}

Then I wrote a user's personal information about the Userbean

public class UserBean {private String name;private string nick;private string face;private string Info;public string Getna Me () {return name;} public void SetName (String name) {this.name = name;} Public String Getnick () {return nick;} public void Setnick (String nick) {this.nick = Nick;} Public String Getface () {return face;} public void Setface (String face) {this.face = face;} Public String GetInfo () {return info;} public void SetInfo (String info) {this.info = info;}  Public UserBean (string name, String nick, String face, String info) {super (); this.name = Name;this.nick = Nick;this.face = Face;this.info = info;} Public UserBean () {super ();}  @Overridepublic String toString () {return "UserBean [name=" + name + ", nick=" + Nick + ", face=" + face+ ", info=" + info + "]";}}


Then through Fast-json.jar parsing:

/** * Get user's personal information * @param JSON * @return */public static UserBean Jsonparseuserbean (String json) {return Json.parseobject (JS On, Userbean.class);}


To be continued; 2014/10/14

Build a group, convenient for everyone to communicate: jumping Android < group number:423923313>

The use of Fast-json.jar

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.