Gson用空字段反序列化 [英] Gson deserializes with empty fields

查看:167
本文介绍了Gson用空字段反序列化的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

结果变量包含已更正的已解析JSON. 但是在反序列化后,List包含正确数量的项目,但所有为空. 如何解决?

The result variable contains corrected parsed JSON. But after deserialization List contains correct amount of items but all of them are empty. How to fix it?

Gson gson = new Gson();
List<UnitView> unitViews = new ArrayList<UnitView>();
// https://stackoverflow.com/questions/5554217/google-gson-deserialize-listclass-object-generic-type
Type typeToken = new TypeToken<List<UnitView>>() { }.getType();
unitViews = gson.fromJson(result,typeToken); 

即使我喜欢

UnitView[] unitViews = gson.fromJson(result, UnitView[].class);

项目的字段也为空.

UnitView

public class UnitView implements Serializable {

    public String id ;

    public String name ;

    public String description ;

    public String deviceTypeName ;


    public String categoryID ;

    public String lastOnline ;

    public String latitude ;

    public String longitude ;

    public String atTime ;

    public String getId() {
        return id;
    }

    public String getName() {
        return name;
    }

    public String getDescription() {
        return description;
    }

    public String getDeviceTypeName() {
        return deviceTypeName;
    }

    public String getCategoryID() {
        return categoryID;
    }

    public String getLastOnline() {
        return lastOnline;
    }

    public String getLatitude() {
        return latitude;
    }

    public String getLongitude() {
        return longitude;
    }

    public String getAtTime() {
        return atTime;
    }

    public void setId(String id) {
        this.id = id;
    }

    public void setName(String name) {
        this.name = name;
    }

    public void setDescription(String description) {
        this.description = description;
    }

    public void setDeviceTypeName(String deviceTypeName) {
        this.deviceTypeName = deviceTypeName;
    }

    public void setCategoryID(String categoryID) {
        this.categoryID = categoryID;
    }

    public void setLastOnline(String lastOnline) {
        this.lastOnline = lastOnline;
    }

    public void setLatitude(String latitude) {
        this.latitude = latitude;
    }

    public void setLongitude(String longitude) {
        this.longitude = longitude;
    }

    public void setAtTime(String atTime) {
        this.atTime = atTime;
    }
}

JSON数据

[{"ID":"294","Name":"Foton Tunland  № F110","Description":null,"DeviceTypeName":"Техника ТО","CategoryID":"18","LastOnline":"19.12.2017 20:38:04","Latitude":"11,40119","Longitude":"11,42403","AtTime":"19.12.2017 20:38:04"},{"ID":"295","Name":"DML LP1200  № 9793","Description":null,"DeviceTypeName":"Буровой станок дизельный","CategoryID":"15","LastOnline":null,"Latitude":null,"Longitude":null,"AtTime":null}]

推荐答案

好,问题是解析器区分大小写,您可以更改属性名称以匹配您可以使用的json值的名称像这样的SerializedName批注:

Ok , the problem is that the parser is case-sensitive, you can change the name of your attributes to match the name of the json value of you could use the SerializedName annotation like this:

@SerializedName("ID")
public String id ;
@SerializedName("Name")
    public String name ;
@SerializedName("Description")
    public String description;
...

    public String ID ;
    public String Name ;
    public String Description ;
...

这篇关于Gson用空字段反序列化的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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