【发布时间】:2018-09-14 14:27:32
【问题描述】:
我有一个返回以下格式的 json 对象的响应:
{
"playerId": "001",
"name": "michel",
"age": 21,
"nation": "USA",
"ratings": [
{
"type": "speed",
"score": "0121"
},
{
"type": "accuracy",
"score": "85"
}
],
"teaminfo": {
"teamName": "HON",
"isValid": "true"
}
}
我有一个 Java 类:
public class MyRider {
public String playerId;
public String name;
public int age;
public String speed;
public String accuracy;
public String teamName;
public String isValid;
//getter, setter...
}
我想使用 GSON 将 JSON 对象映射到 Java 对象。
我尝试使用 JsonDeserializationContext 反序列化,它为 JSON 中的嵌套值返回 null。
【问题讨论】:
-
感谢贾斯汀重构问题。
-
不同格式是什么意思?
-
你能更新你的代码吗?
-
@benjaminc:在 json 中有类型和分数的列表,但在 java 类中它的单个字符串,例如。 type:speed变成string speed,score就是它的值。
-
@deadpool:你需要什么样的更新?
标签: java json gson deserialization