gpt4 book ai didi

java - Gson 使用空字段反序列化

转载 作者:太空狗 更新时间:2023-10-29 14:41:16 27 4
gpt4 key购买 nike

result 变量包含经过更正的已解析 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);

项目的字段也是空的。

单元 View

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}]

最佳答案

好的,问题在于解析器区分大小写,您可以更改属性的名称以匹配您可以使用 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 ;
...

关于java - Gson 使用空字段反序列化,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47898640/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com