使用Gson将JSON映射到POJO [英] Mapping JSON into POJO using Gson

查看:161
本文介绍了使用Gson将JSON映射到POJO的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有以下JSON来表示盐请求的服务器响应:

  {
USER :
{
E_MAIL:email,
SALT:salt
},
CODE:010
}

我试图用下面的POJO来映射它:

  public class SaltPOJO {
private String code = null;
私人用户user = null;

@Override
public String toString(){
return this.user.toString();
}

public String getCode(){
return code;
}
public void setCode(String code){
this.code = code;
}
public User getUser(){
return user;
}
public void setUser(User user){
this.user = user;
}

public class User {
private String e_mail = null;
private String salt = null;

@Override
public String toString(){
return this.e_mail +:+ this.salt;
}
public String getE_mail(){
return e_mail;
}
public void setE_mail(String e_mail){
this.e_mail = e_mail;
}
public String getSalt(){
return salt;
}
public void setSalt(String salt){
this.salt = salt;



$ / code $ / pre

现在每次我都这样做:

  Gson gson = new Gson(); 
SaltPOJO saltPojo = gson.fromJson(json.toString(),SaltPOJO.class);

Log.v(Bla,saltPojo.toString());

saltPojo.toString()为空。如何使用Gson将我的JSON映射到POJO?
是我的变量的顺序对于Gson映射很重要吗?

解决方案



不,情况并非如此。


如何使用Gson将我的JSON映射到POJO?


它是区分大小写和JSON字符串中的键应与POJO类中使用的变量名称相同。



您可以使用@SerializedName 注释可以使用任何变量名称。



示例代码:

  class SaltPOJO {
@SerializedName (CODE)
private String code = null;
@SerializedName(USER)
private User user = null;
...

class用户{
@SerializedName(E_MAIL)
private String e_mail = null;
@SerializedName(SALT)
private String salt = null;


I have the following JSON to represent the server response for a salt request:

{
    "USER":
    {
        "E_MAIL":"email",
        "SALT":"salt"
    },
    "CODE":"010"
}

And i tried to map it with the following POJO:

public class SaltPOJO {
    private String code = null;
    private User user = null;

    @Override
    public String toString() {
        return this.user.toString();
    }

    public String getCode() {
        return code;
    }
    public void setCode(String code) {
        this.code = code;
    }
    public User getUser() {
        return user;
    }
    public void setUser(User user) {
        this.user = user;
    }

    public class User {
        private String e_mail = null;
        private String salt = null;

        @Override
        public String toString() {
            return this.e_mail + ": " + this.salt;
        }
        public String getE_mail() {
            return e_mail;
        }
        public void setE_mail(String e_mail) {
            this.e_mail = e_mail;
        }
        public String getSalt() {
            return salt;
        }
        public void setSalt(String salt) {
            this.salt = salt;
        }
    }
}

Now everytime i do this:

Gson gson = new Gson();
SaltPOJO saltPojo = gson.fromJson(json.toString(), SaltPOJO.class);

Log.v("Bla", saltPojo.toString());

The saltPojo.toString() is null. How can i map my JSON into POJO using Gson? Is the order of my variables important for the Gson mapping?

解决方案

Is the order of my variables important for the Gson mapping?

No, that's not the case.

How can i map my JSON into POJO using Gson?

It's Case Sensitive and the keys in JSON string should be same as variable names used in POJO class.

You can use @SerializedName annotation to use any variable name as your like.

Sample code:

  class SaltPOJO {
        @SerializedName("CODE")
        private String code = null;
        @SerializedName("USER")
        private User user = null;
        ...

        class User {
            @SerializedName("E_MAIL")
            private String e_mail = null;
            @SerializedName("SALT")
            private String salt = null;

这篇关于使用Gson将JSON映射到POJO的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆