作者热门文章
- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我必须解析 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/
我是一名优秀的程序员,十分优秀!