且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

从没有索引的数组将json转换为pojo

更新时间:2023-01-16 20:50:42

创建可序列化的类-

class Response implements Serializable{
    @SerializedName("Response")
    private String response;
    @SerializedName("Message")
    private String message;

    public Response(){}

    public Response(String response, String message){
        this.message = message;
        this.response = response;
    }

    //todo getter and setter methods
}

现在借助Gson解析JSON数据.

String jsonString = "{\"Response\":\"Success\",\"Message\":\"YES\",\"Data\":{\"Info\":{\"id\":\"1\" , \"name":\"leon\"}}}";
Response responseObject = new Gson()
                          .fromJson(
                             jsonString,
                             Response.class
                          );

在上面的此POJO类中,您可以根据需要添加其他数据.要避免序列化和反序列化的任何特定属性,可以使用 排除策略 .

in this POJO class above you can add other data if you want. for avoiding any specific property to serialize and deserialize you can use exclusion strategy.

有关此信息的更多信息,请访问此处.

For more information on this yoou can go here.