gpt4 book ai didi

java - 如何将 3h 解析为 java?(无法设置以数字开头的变量)

转载 作者:行者123 更新时间:2023-12-01 17:46:20 26 4
gpt4 key购买 nike

我必须解析 API 对 java 对象的响应并以 json 格式打印它们。我设法做到了,但是响应的变量是“3h”。我无法在 Java 中命名名为 3h 的变量,因此无法解析 Object.h 中的值。有什么我可以做的吗?我正在使用 com.google.gson.Gson 库。谢谢。

“雨”:{“3h”:3.4375},”

public class Rain{
private float threeh;
public float getThreeh() {
return threeh;
}

public void setThreeh(float threeh) {
this.threeh = threeh;
}
}

最佳答案

您可以查看名为 GSON 的库,特别是使用 @SerializedName 注释。你的类将会变成这样:

public class Rain {
@SerializedName("3h")
float threeh;
}

您可以在此处阅读有关该注释的更多信息:https://google.github.io/gson/apidocs/com/google/gson/annotations/SerializedName.html

关于java - 如何将 3h 解析为 java?(无法设置以数字开头的变量),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54756157/

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