Gson parsing: mapping Json fields to Java fields with different names and mapping multiple Json fields to a single Java field

1. Gson normal analysis

Generally, when we use Gson to parse Json strings into Java entity data classes, the Json fields and Java fields have a one-to-one correspondence and the names are exactly the same. For example, the following Json string:

{
    "userId":3,
    "userName":"张三"
}

The corresponding Java entity class is:

    //只能解析 userId、userName
    public final class UserInfo {
        public int userId;
        public String userName;
    }

One more thing here, the GsonFormat plug-in is not available after Android Studio is upgraded to 4.1.X, you can use the GsonFormatPlus plug-in instead.

2. Map Json fields to Java fields with different names

For the Java entity class above, if the Json string field returned by the background changes at this time:

{
    "USER_ID":4,
    "USER_NAME":"李四"
}

The corresponding foreground also needs to modify the Java entity class fields, followed by modifying the data binding of the View layer. On the Java field, add the @SerializedName annotation to the field of the entity class:

    //只能解析 USER_ID、USER_NAME
    public final class UserInfo {
        @SerializedName("USER_ID")
        public int userId;
        @SerializedName("USER_NAME")
        public String userName;
    }

Note, however, that the entity class can only parse the USER_ID and USER_NAME fields at this time, and cannot parse the userId and userName fields any more.

3. Map multiple Json fields to a single Java field

As mentioned above, if we want to parse the USER_ID and USER_NAME fields, we also need to parse the userId and userName fields without changing the entity class fields. At this point, you need to modify the field annotation of the entity class, and use the value and spare parameters in the @SerializedName annotation:

    //可以解析 userId、userName;USER_ID、USER_NAME
    public final class UserInfo {
        @SerializedName(value = "userId", alternate = "USER_ID")
        public int userId;
        @SerializedName(value = "userName", alternate = "USER_NAME")
        public String userName;
    }

At this time, the instance class can resolve not only the fields USER_ID and USER_NAME, but also the fields userId and userName.

 One step closer:

 Then come the following Json string:

{
    "ID":5,
    "NAME":"王五"
}

If you still want to use the original entity class for parsing, you need to modify the spare parameters in the @SerializedName annotation to a string array, as follows:

    //可以解析 userId、userName;USER_ID、USER_NAME;ID、NAME
    public final class UserInfo {
        @SerializedName(value = "userId", alternate = {"USER_ID", "ID"})
        public int userId;
        @SerializedName(value = "userName", alternate = {"USER_NAME", "NAME"})
        public String userName;
    }

At this time, the instance class can resolve userId, userName; USER_ID, USER_NAME; ID, NAME.

4. Summary:

Use the following entity class with the @SerializedName annotation, which uses values ​​and alternate parameters, and the alternate parameters are string arrays:

    //可以解析 userId、userName;USER_ID、USER_NAME;ID、NAME
    public final class UserInfo {
        @SerializedName(value = "userId", alternate = {"USER_ID", "ID"})
        public int userId;
        @SerializedName(value = "userName", alternate = {"USER_NAME", "NAME"})
        public String userName;
    }

Json of the following three fields can be parsed at the same time:

    String json1 = "{\"userId\":3,\"userName\":\"张三\"}";
    String json2 = "{\"USER_ID\":4,\"USER_NAME\":\"李四\"}";
    String json3 = "{\"ID\":5,\"NAME\":\"王五\"}";

Format Json as follows:

{
    "userId":3,
    "userName":"张三"
}

{
    "USER_ID":4,
    "USER_NAME":"李四"
}

{
    "ID":5,
    "NAME":"王五"
}

 

Guess you like

Origin blog.csdn.net/beita08/article/details/116046901