Android Development, Docking Background Server, Getting Started with JSON Resolution

Keywords: JSON Google Gradle

First of all, the tool we're using here is Google encapsulated Gson. This post was written for gson 2.7 and the following dependencies need to be added to Gradle.build when calling

compile 'com.google.code.gson:gson:2.7'

Here are some concepts explained in this json string

{
    "status": [],
    "message": "logout successfully",
    "data": [
        {
            "pic_dir": "uploads/touxiang/20170624/fc4c604edaf2f32f6f5b4af939337d38.jpg",
            "fensi": 0,
            "guanzhu": 0
        },
        {
            "pic_dir": "uploads/touxiang/20170624/fc4c604edaf2f32f6f5b4af939337d38.jpg",
            "fensi": 0,
            "guanzhu": 0
        }
    ]
}

Now we're going to break down the elements inside them, and we need to remember

There can be multiple or no JsonObjects in JsonArray and multiple or no JsonElement s in JsonObject.
Each node can be either a JsonObject or a JsonArray;

Fundamentals: Because in the process of learning to use, you need to know these three basic concepts:
(1)JsonArray, by [...]Enclosed whole part
(2)JsonObject, by {} Enclosed whole part
(3)JsonElement, where each node is a JsonElement, for example, "status": 1

The following string of Jsons, the data I want to go to "pic_dir", can be associated with a JsonElement of JsonObject in a JsonElement in a JsonObejct;

Summary: Three essential elements: you have me, I have you

{
    "data": [
        {
            "pic_dir": "uploads/touxiang/20170624/fc4c604edaf2f32f6f5b4af939337d38.jpg",
            "fensi": 0,
            "guanzhu": 0
        },
        {
            "pic_dir": "uploads/touxiang/20170624/fc4c604edaf2f32f6f5b4af939337d38.jpg",
            "fensi": 0,
            "guanzhu": 0
        }
    ]
}

1. The first common Json format

{
    "status": 1,
    "message": "logout successfully",
    "data": []
}

Related code (keep and return the name of the Json string consistent)

public class LoginResult {
    private String status;
    private String message;
    private String data;

    public LoginResult(String status, String message, String data) {
        this.status = status;
        this.message = message;
        this.data = data;
    }

    public String getStatus() {
        return status;
    }

    public void setStatus(String status) {
        this.status = status;
    }

    public String getMessage() {
        return message;
    }

    public void setMessage(String message) {
        this.message = message;
    }

    public String getData() {
        return data;
    }

    public void setData(String data) {
        this.data = data;
    }
}

Parsing method, get/set method of ViewModel class to get data corresponding to a JsonElement

//Encapsulate it as an object of a class using tools
Gson gson = new Gson();
//fromJson This method converts a json string into a ViewModel if the Filed of the ViewModel wants to
//Consistency of json strings
LoginResult result = gson.fromJson(sb.toString(), LoginResult.class);

For example, get the message node in json

Logger.d(result.getMessage());

2. The second common Json format

{
    "status": 1,
    "message": "query success",
    "data": {
        "pic_dir": "uploads/touxiang/20170624/fc4c604edaf2f32f6f5b4af939337d38.jpg",
        "fensi": 0,
        "guanzhu": 0
    }
}

notes: In this case, the data under the data node corresponds to JsonObject, and the parsing method behind knowing this is clear, and the entire Json string is a JsonObject.

Related Codes

public class ViewModelResult {
    private String status;
    private String message;
    private JsonObject data;

    public ViewModelResult(String status, String message, JsonObject data) {
        this.status = status;
        this.message = message;
        this.data = data;
    }

    public String getStatus() {
        return status;
    }

    public void setStatus(String status) {
        this.status = status;
    }

    public String getMessage() {
        return message;
    }

    public void setMessage(String message) {
        this.message = message;
    }

    public JsonObject getData() {
        return data;
    }

    public void setData(JsonObject data) {
        this.data = data;
    }
}

Parse related code

Logger.d(receiverJsonString);
ViewModelResult result = gson.fromJson(receiverJsonString,ViewModelResult.class);
JsonObject dataStr = result.getData();
Logger.d(dataStr.toString());
...
JsonObject receiveJson;
FansViewModel viewModel;
//Converts an entire json string into a json object
receiveJson = dataStr;
viewModel = new FansViewModel();
viewModel.setPic_dir(receiveJson.get("pic_dir").getAsString());
viewModel.setFensi(receiveJson.get("fensi").getAsInt());
viewModel.setGuanzhu(receiveJson.get("guanzhu").getAsInt());
viewModels.add(viewModel);

3. The third Json format

{
    "status": 1,
    "message": "logout successfully",
    "data": [
        {
            "pic_dir": "uploads/touxiang/20170624/fc4c604edaf2f32f6f5b4af939337d38.jpg",
            "fensi": 0,
            "guanzhu": 0
        },
        {
            "pic_dir": "uploads/touxiang/20170624/fc4c604edaf2f32f6f5b4af939337d38.jpg",
            "fensi": 0,
            "guanzhu": 0
        }
    ]
}

Parse the code to get "pic_dir", the principle is that the third JsonElement of the ViewModel parsed by the fromJson method is a JsonArray, which first obtains the JsonObject by the JsonArray method and then obtains the JsonElement you want.The method will not be redundant.

//Key Methods
jsonArray.get(i).getAsJsonObject();
jsonObject.get(i).getAsString();

This is some of my opinions. God forbids me. If there are any errors, they will be corrected in time.

Posted by sarakmo on Wed, 19 Jun 2019 09:12:01 -0700