5 minutes for you to learn to parse all JSON with the most efficient tools

Source: Internet
Author: User
Tags string format

If you are an Android developer, learning to parse JSON strings is a required course for you, this article will teach you how to do this in an instance, and spend five minutes reading this article so you can learn to parse all the JSON strings.

Get ready:

        • JSON string
        • Fastjson
        • Hijson Formatting JSON tool

Start Tutorial:

        • Fastjson:

          Commonly used to parse the JSON tool class has Google's Gson,jackson,fastjson, here does not do a comparison, Bo Master told everyone, Fastjson is the most efficient best use, choose it is right. Fastjson from the hands of Ali engineers, is a JSON processing toolkit, including the "serialization" and "deserialization" two parts, it has the following characteristics:

          • The fastest, test shows that Fastjson has a very fast performance beyond any other Java Json parser. Including claiming the fastest Jackjson, is the Gson resolution speed of 6 times times;

          • Powerful, fully support Java Bean, collection, Map, date, Enum, support paradigm, support introspection, no dependency, able to run directly in Java SE version 5.0 or above, support Android, open source (Apache 2.0)

          [fastjson jar包](http://download.csdn.net/detail/pdsyzbaozi/8199419)
        • Hijson:

          Hijson is a JSON string format tool, very useful, so that your JSON string structure at a glance, and can directly copy the key value, highly recommended!

          Hijson

The Fastjson API Ingress class is Com.alibaba.fastjson.JSON, and common serialization operations can be done directly on a static method on a JSON class.

12345678 public static final Object parse (String text); Parse the JSON text to Jsonobject or jsonarraypublic static final Jsonobject parseobject (String text); Parse the JSON text into jsonobjectpublic static final T parseobject (String text, Class clazz); Parse the JSON text as javabeanpublic static final Jsonarray Parsearray (String text); Parse the JSON text into jsonarraypublic static final List Parsearray (String text, Class clazz); Parse the JSON text into JavaBean set public static final String tojsonstring (Object object); Serializes JavaBean to JSON text public static final String tojsonstring (Object object, Boolean Prettyformat); Serializes JavaBean to formatted JSON text public static final object ToJSON (object javaobject); Convert JavaBean to Jsonobject or Jsonarray.

If you have never parsed JSON, do not understand it is OK, now I have the JSON string above, hands-on teaching you how to parse, learn to parse this more complex JSON string, I believe other you will certainly also parse.

JSON string for everyone to take to practiced hand

12345678910111213141516171819202122232425262728293031 {"Status": "+", "msg": "successful!", "data": [{"Details": [{"Distance": 2847, "Nextlat": 39.994076, "Nextlong": 116.47764, "Nexti": "Melodev", "Status": 4}], "Distance": 2847, "IMAGEURL": "", "Overview": "Long-term original Android Blog", "Source": " Http://www.jianshu.com/users/f5909165c1e8/latest_articles "," status ":" Successful "}, {" Details ": [{" Distance ": 2769 , "Nextlat": 39.97691, "Nextlong": 116.46019, "Nexti": "Melodev", "Status": 4}], "Distance": 2769, "IMAGEURL": "", " Overview ":" Like please add attention "," source ":" Http://www.jianshu.com/users/f5909165c1e8/latest_articles "," status ":" Successful "} ]}

All right, ready to go!

Import the downloaded Fastjson two jar package into the Libs directory:

Now start a more critical step, create a new Bean object, as a vector after JSON parsing, the code is as follows:

1234567891011121314151617181920212223242526272829303132333435363738 public class Queryresultinfo {public String status;public string msg;public list<datalist> data; public class DataList {public int distance;public string Imageurl;public string Overview;public string Source;public Strin G status;public list<detailslist> details; @Overridepublic String toString () {return "DataList [distance=" + Distance + ", imageurl=" + ImageUrl + ", overview=" + Overview + ", source=" + source + ", status=" + Status + ", details= "+ details.tostring () +"] ";} public class Detailslist {public int distance;public double Nextlat;public double nextlong;public String nexti;public int status; @Overridepublic String toString () {return "detailslist [distance=" + Distance + ", nextlat=" + Nextlat + ", Nextlon G= "+ Nextlong +", nexti= "+ Nexti +", status= "+ Status +"] ";}}} @Overridepublic String toString () {return "Queryresultinfo [status=" + Status + ", msg=" + msg + ", data=" + data.tostring () + "]";}}

Let me tell you, write a technique that parses the Bean object after the JSON. First look at the JSON formatted results (Hijson tool right view), in Java code:

        • The type of the data, the name of the key must correspond to the JSON string guarantee one by one
          In the example, each variable is named after the key name of the JSON, cannot be written wrong, and the data type must correspond, string is String,int is Int,float is float

        • If nested arrays are present, write an inner class , name each JSON field in the same way, and receive it with the list, noting that the name of the list is also the name of the key in JSON. Multiple layers of nesting and so on.

        • The access domain for all variables is public.

Well, the Bean object is done.

I put the JSON string in the String.xml, after clicking the button, parse the JSON string, layout file is very simple, here does not release.

As can be seen, we call Json.parseobject (Myjson,appinfo.class) This line of code, we put all the information of the JSON string into the AppInfo object, want to use what can be directly removed.

This JSON string is relatively complex, multi-layered nesting, so this you will be, simple you also certainly no problem, of course, the strength of fastjson more than this, if there are special needs, then slowly explore it!

5 minutes for you to learn to parse all JSON with the most efficient tools

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.