Gson没有从json字符串格式化一个变量

时间:2014-11-05 11:54:59

标签: java json arraylist gson

我正在尝试将json格式(来自rest webservice的arraylist响应)转换为相应的java arraylist。

令人惊讶的是,3个变量中只有2个被格式化为对象,3个变量被设置为null。

以下是字符串格式的json响应。

[{"points":"20","shares":"54","name":"Krishna"},{"points":"18","shares":"47","name":"Bhima"}]

我正在尝试使用以下代码转换为arraylist:

    ArrayList<GplusFriend> gp_list;
    Gson gson = new Gson();
    gp_list = gson.fromJson(result, new TypeToken<List<GplusFriend>>(){}.getType());

 Iterator itr = gp_list.iterator();
            GplusFriend gf =null;
            while(itr.hasNext()){
                gf= (GplusFriend) itr.next();
                Log.d("Restcall", "Name :"+gf.getName());
                Log.d("Restcall", "points :"+gf.getPoints());
                Log.d("Restcall", "shares :"+gf.getShares());
                }

但我得到的日志是:

Name :null
points :20
shares :54
Name :null
points :18
shares :47

这是GPlusFriend的类定义:

public class GplusFriend {

    String Name;

    String points;
    String shares;
    public String getShares() {
        return shares;
    }
    public void setShares(String shares) {
        this.shares = shares;
    }
    public String getName() {
        return Name;
    }
    public void setName(String name) {
        Name = name;
    }

    public String getPoints() {
        return points;
    }
    public void setPoints(String points) {
        this.points = points;
    }


    public GplusFriend(){
        super();
    }
    public GplusFriend(String Name,String points,String shares){
        super();

        this.shares=shares;
        this.Name = Name;
        this.points = points;
    }

    @Override

    public String toString(){

        return this.Name + "     "+this.points+"     "+this.shares;

    }
}

所以我在这里失踪了..

1 个答案:

答案 0 :(得分:1)

应该是

String name;

而不是

String Name;

您的JSON密钥是小写字母&#34;名称&#34; 使用GSON时,变量名称区分大小写