In depth analysis of Android JSON analysis

  • 2020-11-30 08:35:01
  • OfStack

JSON grammar

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

The value of JSON can be:

Number (integer or floating point number)

String (in double quotes)

Logical value (true or false)

Array (surrounded by square brackets [])

Object (surrounded by curly braces {})

null

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

1. Object: Object in js is represented as the content enclosed by "{}". The data structure is {key: value,key: value... In object-oriented languages, key is the property of the object, and value is the corresponding property value, so it is easy to understand that the value method is the object.key gets the property value, and the type of the property value can be Numbers, strings, arrays, objects.

2, array: Array in js is the content enclosed by brackets "[]", data structure is [" java ", "javascript", "vb"...] , the value method is the same as in all languages, using index to get, the field value type can be number, string, array, object several.

1 set for a Android network programming classmates JSON parsing is not all strange 1 point, because now we request to the server resources, mobile phone server give us the returned data resources are JSON format returned 1, and of course some return through XML format, 1 relative JSON format, XML format on data processing is relatively complicated, and Android provides us with two parsing JSON object classes: JSONObject and JSONArray that two objects can be very good to satisfy our needs, JSONArray object can be in the form of an array of data back to the mobile phone, JSONObject object can be in the form of object data encapsulation for us good returns, mobile phone after receiving all these two kinds of data, by parsing, can be very convenient use, greatly facilitates the development of our learning.

Of course, when JSONObject is used for data parsing, there are two ways: one is to fetch data in the form of key-value pairs one by one; The other one is to JSONObject resolved to a specific object, and then through the object get, set method for data reading and operation, for the first kind of way, to believe that just small contact JSON parsing of children's shoes are so dry, relative way 1, 2 kind of way is more simple and convenient, let's 1 l learn how to implement JSONObject object into a specific object.

Because today we are going to discuss the parsing of JSONObject, I will introduce how to transform JSONObject to concrete objects through a simple object creation -- object encapsulation -- object parsing -- object operation.

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 rewrote the toString method of object, here need to pay attention to the format of the return parameter: the name of the class + '[' parameter name + +' = '+... + + parameters']'

Here is our object creation and resolution:

1. JSONObject object resolution 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. Creation and resolution of JSON objects:


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 of our object properties JSONObject In the 
    JSONObject jo = new JSONObject();
    jo.put("id", "16");
    jo.put("name", " The small ");
    jo.put("from", " henan ");
    String str = jo.toString();
    Toast.makeText(this, str, Toast.LENGTH_LONG).show();
    // right JSONObject Object parsing 
    User u = (User) new JsonParseToObject().AllJsonParseToObject(jo.toString(), "com.example.jsontoobject.User");
    // judge JSONObject Object resolution is correct 
    if(u != null){
      text.setText(jo.toString()+"\nid:"+u.getId()+";name:"+u.getName()+";from:"+u.getFrom());// Perform object manipulation 
    }else{
      text.setText("User == null");
    }
  }
}

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

That's all we have to say about JSONObject parsing. If you haven't tried this method before, you can try it. It is very convenient.

The above is the full description of the Android JSON parsing Shared on this site, hoping to help you.


Related articles: