An in-depth analysis of the Android JSON parsing _android

Source: Internet
Author: User

JSON syntax

First look at the syntax and structure of JSON so that we know how to parse it. A JavaScript object represents a subset of the syntax when JSON syntax.

The value of the JSON can be:

Number (integer or floating-point)

String (within double quotes)

Logical value (TRUE or FALSE)

Array (surround with square brackets [])

Object (surrounded with curly braces {})


There are only two structures in JSON: objects and arrays.

1, object: Object in JS is expressed as "{}" enclosed content, data structure is {Key:value,key:value,...} The structure of key-value pairs, in object-oriented language, key is the object's attribute, value is the corresponding attribute value, so it is easy to understand that the value method is the object. Key gets the value of the property, which can be a number, a string, an array, and several objects.

2, array: The array in JS is in the brackets "[]" enclosed content, the data structure is ["Java", "JavaScript", "VB",...], as in all languages, the type of field value can be numbers, strings, arrays, objects, and so on.

Do an Android network programming students must be not unfamiliar to JSON parsing, because now we use mobile phone to the server to request resources, the server returned to us the data resources are generally returned in JSON format, of course, some of the return through the XML format, relative to the JSON format, XML-formatted data is relatively cumbersome to process, and Android provides us with two classes to parse JSON objects: Jsonobject and Jsonarray can meet our needs very well, Jsonarray object can be an array of data returned to the mobile phone, Jsonobject object can be the form of the object of the data for us to package good return, the phone received both data, through the analysis, can be very convenient to use, greatly facilitate our development and learning.

Of course, in the use of Jsonobject data parsing, there are two ways, one through the form of key-value pairs of data, the other is to parse the Jsonobject into specific objects, and then through the object's get, set method for data reading and operation, for the first way, I believe that just contact with the young children's shoes are in the same way, the first method, the second way more simple and convenient, below we will learn how to achieve the Jsonobject object into a specific object bar.

Because today we are going to discuss about jsonobject parsing, so I am through a simple object creation-Object encapsulation-Object parsing-object operation, to introduce you how to Jsonobject to the specific object of the transformation.

First of all, our object class (user):

public class User {
  private String ID;
  private String name;
  Private String from;
  Public String GetId () {return
  public void SetId (String id) { = ID;
  Public String GetName () {return
  public void SetName (String 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 rewrite the object's ToString method, here must pay attention to return the parameters of the format: class name + ' [' +] + ' = ' + parameter +.....+ '] '

Here is our object creation and resolution:

  1, Jsonobject object resolution class:

Jsonobject resolution 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, the creation and resolution of JSON objects:

public class Mainactivity extends activity {
  protected void onCreate (Bundle savedinstancestate) {
    super.oncreate (savedinstancestate);
    Setcontentview (r.layout.activity_main);
    TextView Text = (TextView) Findviewbyid (R.ID.HW);
    Our object attribute encapsulation is jsonobject
    jsonobject jo = new Jsonobject ();
    Jo.put ("id", "All");
    Jo.put ("name", "small");
    Jo.put ("from", "Henan");
    String str = jo.tostring ();
    Toast.maketext (this, str, toast.length_long). Show ();
    Resolves the Jsonobject object
    user U = (user) new Jsonparsetoobject (). Alljsonparsetoobject (Jo.tostring (), "Com.example.jsontoobject.User");
    Determines whether the Jsonobject object resolves correctly
    if (U!= null) {
      text.settext (jo.tostring () + "\nid:" +u.getid () + "; Name:" +u.getname ( + "; From:" +u.getfrom ());////
      text.settext ("User = = null");}

A layout file is simply a text box that displays the parsed data.

All right, here we are. About Jsonobject analytic knowledge for everyone to introduce finished, no use this way of small partners can try, or quite convenient, if you have a better way, but also hope to exchange learning.

These are all the details of the Android JSON parsing that we've been sharing, and I hope we can help.

Related Article

E-Commerce Solutions

Leverage the same tools powering the Alibaba Ecosystem

Learn more >

Apsara Conference 2019

The Rise of Data Intelligence, September 25th - 27th, Hangzhou, China

Learn more >

Alibaba Cloud Free Trial

Learn and experience the power of Alibaba Cloud with a free trial worth $300-1200 USD

Learn more >

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: and provide relevant evidence. A staff member will contact you within 5 working days.