gpt4 book ai didi

java - Gson().toJson(this) 无法正常工作

转载 作者:行者123 更新时间:2023-12-01 11:09:02 24 4
gpt4 key购买 nike

我正在尝试通过 GSON 库将我的对象转换为字符串。我这样写: Gson().toJson(this) 和 String 我收到的不完整。这是我的“this”对象:

http://i.snag.gy/LgXrw.jpg

如您所见,它有精度字段(结果 -> 轨迹 -> 点 -> 精度)但是当我执行这个 Gson().toJson(this) 时,我得到了没有这个字段的字符串。这是怎么回事?

{
"carId": 14,
"closed": 1442409732,
"commentEnd": "оо",
"guid": "bbc329e4-65c5-4b16-91d5-45283ca9dcb7",
"isDeleted": false,
"lastUpdate": 0,
"litersEnd": -23.1,
"litersStart": -23.1,
"number": 0,
"odometrEnd": 25,
"odometrStart": 44,
"opened": 1442409723,
"id": 0,
"status": 2,
"Tracks": [
{
"categoryId": 2,
"closed": 1442409729,
"commentEnd": "рр",
"distance": 0,
"guid": "aba4de4a-5bc7-49fc-a537-b8da3dd17783",
"opened": 1442409723,
"points": [
{
"dateCreated": 1442409725,
"guid": 579,
"lat": 56.8408432,
"lng": 53.2100482
}
],
"id": 0,
"statusNumber": 2
}
],

}

我的 Point 对象类看起来像这样(只是变量声明:

@DatabaseTable(tableName = "points")
public class Point implements Serializable {

@DatabaseField(generatedId = true)
@SerializedName("guid")
long id;
@DatabaseField(foreign = true, columnName = "trackId")
transient Track track;
@DatabaseField
double lat;
@DatabaseField
double lng;
@DatabaseField
long dateCreated;
@DatabaseField
transient float accuracy; //this field is faulty
transient String provider;

public Point() {}

public Point(double lat, double lng, long dateCreated) {
this.lat = lat;
this.lng = lng;
this.dateCreated = dateCreated / 1000L;
}

}

PointTrack对象的字段,而Track对象是DrivingList对象的字段,它扩展了Uploadable我在其中运行此代码的抽象类:

public abstract class Uploadable {

public String toJson() {
return new Gson().toJson(this);
}

最佳答案

抱歉,我的类(class)中的这个字段是暂时的。

Java中的transient关键字用于指示字段不应被序列化。

关于java - Gson().toJson(this) 无法正常工作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32587554/

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