Gson用空字段反序列化

时间:2017-12-20 04:07:12

标签: java android gson json-deserialization

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

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 DATA

[{"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}]

2 个答案:

答案 0 :(得分:2)

好的,问题是解析器区分大小写,您可以更改属性的名称以匹配您可以使用SerializedName注释的json值的名称,如下所示:

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

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

答案 1 :(得分:-2)

我认为你因为json中的空值而遇到了这个问题。 核实。 Source