Previously, in the project requirements discussion – Special JSON processing returned from the background, WE talked about how to handle the case that the Key in JSON is a variable value on the Android side.

This time it continues to be the Json data passed from the background encountered in the actual project. Let’s take a look at what is passed to us from the background this time.

{
"success": true."msg": "{' Company abbreviation ':[],' Year List ':[\"2016\",\"2015\",\"2014\"]}"."obj": null
}Copy the code

Yeah, the requirement this time is, it’s a chart showing, tell me which companies, which years have data. Then I choose the year of 2016 of company A, which can be given to the background, and the background can return to other content.

First of all, we can see that the JSON returned to us is a mess. You might say, well, I’m sure I’ll come off the stage, but I don’t need to know how to deal with all this messy JSON parsing. That’s not really worth looking at. Ha ha. It must be the easiest and fastest way to call a background change.

Let’s talk about this json problem:

  1. In general (probably in my projects up to now) we put the main data information in obJ, and MSG will return something like you failed to get the data. A written explanation that may give you a reason.

  2. We see behind the scenes that the JSON portion of the actual useful data is rewrapped as a string, which is then used as the value of the MSG.

  3. JSON has a backslash “\”.

  4. The Key value is in Chinese. Company profile, etc.

The first problem is not dealt with, which may be different from each company. We mainly deal with 2,3 and 4 problems, and then smoothly get the useful data from the background in Android.


Ok, so let’s step by step make this JSON available on Android.

The first step:

{
"success": true."msg": "{' Company abbreviation ':[],' Year List ':[\"2016\",\"2015\",\"2014\"]}"."obj": null
}Copy the code

We could see {‘ company referred to as’ : [], ‘year list: [\ “2016 \” and \ “2015 \”, \ “2014 \”]} is a string, so we can put this as a normal string. For example, consider the following:

{
"success": true."msg": "XXXX"."obj": null
}Copy the code

If let you take down the XXXX content. I think everyone can do it

For example, I created an entity class (using the GsonFormat plugin, of course) :

public class AYearBean {


    / * * * success: true * MSG: {' company referred to as' : [], 'year list: [" 2016 ", "2015", "2014"]} * obj: null * /

    private boolean success;
    private String msg;
    private Object obj;

    public boolean isSuccess(a) {
        return success;
    }

    public void setSuccess(boolean success) {
        this.success = success;
    }

    public String getMsg(a) {
        return msg;
    }

    public void setMsg(String msg) {
        this.msg = msg;
    }

    public Object getObj(a) {
        return obj;
    }

    public void setObj(Object obj) {
        this.obj = obj; }}Copy the code

I used Retrofit2 + Rxjava2. So it can be written as:

 @GET("kpi/getBasicDataForUser.do")
  Observable<AYearBean> getYear();Copy the code

So we have the AYearBean object (assuming the object reference is a bean). We take {‘ company referred to as’ : [], ‘year list: [\ “2016 \” and \ “2015 \” and \ “2014 \”]} this content, only need to bean. The getMsg () method.

The second step:

We see the MSG value we got: {‘ company name ‘:[],’ year list ‘:[\”2016\”,\”2015\”,\”2014\”]}, which is also a standard JSON format, but it has backslashes in it, so we get the value and remove the backslashes. This one is very simple, bean.getmsg ().replace(“\\”,””). Call the replace method and we get rid of the backslash. So here we go. We get the value value of the MSG is {‘ company referred to as’ : [], ‘year list: [” 2016 “, “2015”, “2014”]}.

Step 3:

We have two ways to do it, depending on which one you prefer:

  1. Continue building the object class. Then convert the value directly to an object and fetch the data:

    Continue to use the GsonFormat plugin{' Company name ':[],' Year List ':["2016","2015","2014"]}Throw it in and generate the object class.
public class BYearBean {

    private List<String> Company abbreviation;private List<String> Year list;public List<String< p style = "max-width: 100%; clear: both; min-height: 1em;returnCompany abbreviation; }public void setCompany Name (List<String> company abbreviation) {this. Company abbreviation = company abbreviation; }public List<String> get list of years () {returnYear list; }public void setList of years (List<String> Year list) {this. Year list = year list; } @Overridepublic String toString() {
        return "BYearBean{" +
                "Company abbreviation ="+ company abbreviation +", year list ="+ Year list +'} '; }}Copy the code

Everyone read !!!! correctly Right, because key is Chinese, the property names here are Chinese !!!! It is so strange, and there is no problem !!!! It’s just that we rarely go there!!

And then we’re gonna go straight to

Gson gson =  new Gson();
BYearBean  subBean = gson.fromJson(bean.getMsg().replace("\\",""),BYearBean.class);
subBean .get Year list ();
subBean .get Company ();Copy the code

2. If you don’t want to change this to an object class with the property name of Chinese, it’s easy. We can just fetch the JSON via key-value.

JsonElement jsonElement =  jsonParser.parse(bean.getMsg().replace("\\",""));
JsonObject msgObject = jsonElement.getAsJsonObject();

Set<Map.Entry<String, JsonElement>> subEntrySet = msgObject.entrySet();

for (Map.Entry<String, JsonElement> subEntry : subEntrySet) {
    if("List of years".equals(subEntry.getKey())){
        JsonArray array = subEntry.getValue().getAsJsonArray();
        ArrayList years = gson.fromJson(array, ArrayList.class);                                              }}Copy the code

In this way, we got the corresponding content smoothly.


Hey, that’s the end of this issue… O ~ O (studying studying)