Android JSON parsing, androidjson

Source: Internet
Author: User

Android JSON parsing, androidjson

The Android network programming personnel must be familiar with JSON parsing, because now we request resources from the server through the mobile phone, and the data resources returned by the server to us are generally returned in JSON format, of course, there are also some data returned in XML format, which is relatively complicated to process in JSON format and XML format, and Android provides us with two classes for parsing JSON objects: JSONObject and JSONArray can meet our needs. JSONArray objects can return data to mobile phones in the form of arrays, the JSONObject object can encapsulate and return the data in the form of an object. After receiving the data, the mobile phone can easily use it through parsing, it greatly facilitates our development and learning.

Of course, when using JSONObject for data parsing, there are two methods: one is to retrieve data one by one through a key-value pair; the other is to parse JSONObject into a specific object, then, read and operate the data through the get and set methods of the object. For the first method, I believe that the kids shoes that just came into contact with JSON parsing work in this way, compared with the first method, the second method is simpler and more convenient. Let's take a look at how to convert a JSONObject to a specific object.

Today we are going to discuss the parsing of JSONObject, so I will use a simple object creation-object encapsulation-object parsing-object operations, this section describes how to convert a JSONObject to a specific object.
First, our object class (user ):

public class User {        private String id;    private String name;    private String from;        public String getId() {        return id;    }    public void setId(String id) {        this.id = id;    }    public String getName() {        return name;    }    public void setName(String name) {        this.name = name;    }    public String getFrom() {        return from;    }    public void setFrom(String from) {        this.from = from;    }        @Override    public String toString() {        return "User [id=" + id + ",name=" + name + ",from=" + from +"]";    }}

Here I overwrite the toString method of the object. Here, you must note the format of the returned parameter: class name + '[' + parameter name + '=' + parameter + ..... + ']'

The following describes how to create and parse an object:

1. JSONObject Object Parsing class:

// JSONObject parsing class public class JsonParseToObject {public Object AllJsonParseToObject (String json, String packageAddress) {Object parseObject = null; try {parseObject = JSON. parseObject (json, Class. forName (packageAddress);} catch (ClassNotFoundException e) {e. printStackTrace ();} return parseObject ;}}

2. Create and parse a JSON object:

Public class MainActivity extends Activity {@ Override protected void onCreate (Bundle savedInstanceState) {super. onCreate (savedInstanceState); setContentView (R. layout. activity_main); TextView text = (TextView) findViewById (R. id. hw); // encapsulate all our object attributes in JSONObject. JSONObject jo = new JSONObject (); jo. put ("id", "16"); jo. put ("name", "small"); jo. put ("from", "Henan"); String str = jo. toString (); Toast. makeText (t His, str, Toast. LENGTH_LONG ). show (); // parse the JSONObject User u = (User) new JsonParseToObject (). allJsonParseToObject (jo. toString (), "com. example. jsontoobject. user "); // determine whether the JSONObject object is correctly parsed if (u! = Null) {text. setText (jo. toString () + "\ nid:" + u. getId () + "; name:" + u. getName () + "; from:" + u. getFrom (); // perform object operations} else {text. setText ("User = null ");}}}

A Layout file is a text box that displays parsed data.

  

Now, we have finished introducing the knowledge of JSONObject parsing. If you do not use this method, you can try it easily. If you have a better method, I also hope to exchange and learn.

Related Article

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.