JSON data stream parsing - local Gson rebuilds Json and then converts the object

In the development process of making a chasing APP, we need to send a request to the server and then the server returns the data. We build our own object locally based on the returned data, which is the general business process. The server returns data in Json format, so we first need to understand what kind of format Json is.

The so-called Json (JavaScript Object Notation) is a lightweight data exchange format. JSON is in a completely language-independent text format, and these features make JSON an ideal data-interchange language. Easy to read and write by humans, but also easy to parse and generate by machines.

Json infrastructure

Json has two structures:
1. A collection of name/value pairs. In different languages, it is understood as an object, a record, a struct, a dictionary, a hash table, a keyed list, or an associative array. array)
2. An ordered list of values. In most languages, it is understood as an array (array)

Regarding the structure of Json, I may write a blog to explain it in the future, and mainly introduce our business process here. The data request and return format agreed by the server are first posted below.

推荐摄影师和模特列表

url;服务器地址:端口/recommend/reclist

| 请求参数    | 说明    |      |
| ------- | ----- | ---- |
| type    | 10850 |      |
| authkey | 用户验证  |      |
|         |       |      |
10850返回样例

说明:

ucimg 是作品集列表图片

Ucfirstimg 是作品集首个图片(200*200)

Uheadimg 用户头像

UserModel 用户模型
{
  "code": "10850", 
  "contents": [
    {
      "Ucimg": [
        "http://oci8c6557.bkt.clouddn.com/哈哈哈哈?e=1492610591&token=yzAza_Cm87nXkh9IyFfpg7LL7qKJ097VK5IOpLj0:LbZjP_K_CC0Bjk6gsArrlM1KY5w="
      ], 
      "UcFistimg": "http://oci8c6557.bkt.clouddn.com/哈哈哈哈?imageView2/1/w/200/h/200&e=1492610591&token=yzAza_Cm87nXkh9IyFfpg7LL7qKJ097VK5IOpLj0:hmheeFyohXVjZ7kyb0mQyGJmK3U=", 
      "Uheadimg": "http://oci8c6557.bkt.clouddn.com/呵呵呵?e=1492610591&token=yzAza_Cm87nXkh9IyFfpg7LL7qKJ097VK5IOpLj0:XXB6WwnL5AgRXT98JkumX-lExBI=", 
      "UserModel": {
        "Uid": 117, ******
        "Uage": 0, ******
        "Ucategory": 2, 
        "Uphone": "158", 
        "Usex": 1, ******
        "Ualais": "dl"
      }
    }
  ]
}

It can be seen that the request and return of the entire data are shown above. The returned content is in Json format. In this Json format, there are two layers, namely code and contents, among which, there are several layers in contents. Next, I will use Gson to reconstruct Json. The so-called reconstruction is actually parsing.

Gson is an open source library of Google, used to parse Json data, very easy to use. Since Gson has a unified method call, many people build their own tool classes in order to be more suitable for their own programs. We directly use the native Gson method here.

Gson gson = new Gson();
JSONArray data = object.getJSONArray("contents");
for(int i = 0; i< data.length(); i++){
    JSONObject info = data.getJSONObject(i);
    model = new RecommandModel();
    model = gson.fromJson(info.toString(),RecommandModel.class);
    mOurList.add(model);
}

Here is a point to see that after getJSONArray() first, getJSONObject is used in the loop, because there is not only this array in the returned contents, but also many recommended people, each of whom is a UserModel, so get the contents first. Large arrays, and then extract small arrays from it in turn.

Among them, we can see that the Json parsed by Gson is saved to the local model object, so how is this model object created? Here, the model object is required to be created according to the return of the server.

import java.io.Serializable;
import java.lang.reflect.Array;
import java.util.ArrayList;

/**
 * Created by fengchi on 2017/4/25.
 */

public class RecommandModel implements Serializable{
    private ArrayList<String> Ucimg;
    private String UcFirstimg;
    private String headimg;
    private UserModel userpublish;
    ...getter/setter方法(command+N自动生成)
}
public class UserModel implements Serializable{
    private String sex;
    private String age;
    private String nickName;
    private String id;
    private String Ucategory;
}

The entire creation is created according to the field returned by the server. It should be noted that the variable name of the model must be strictly consistent with the return, that is, the case must be consistent. In this way, Gson will automatically store the data in the model object, which is really convenient.

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=324697054&siteId=291194637