给定以下 Json 数据:
"values": [
{
"type": "any",
"value": "MO918038B",
"key": "nino"
},
{
"type": "any",
"value": "1956-11-18",
"key": "dob"
},
{
"type": "any",
"value": "q",
"key": "memorableWord"
},
{
"type": "any",
"value": "E13468",
"key": "pin"
},
]
以及以下 Java 类:
public void doStuff() {
try (Reader reader = new FileReader("global-vars.json")) {
JsonObject json = new Gson().fromJson(reader, JsonObject.class);
String reg = json.get("values").toString();
System.out.println(reg);
} catch (IOException e) {
e.printStackTrace();
}
}
搜索后,我不清楚如何最好地获取“nino”的“值”,即=“MO918038B”
最佳答案
Type listType = new TypeToken<List<YourObjectClass>>() {}.getType();
List<YourObjectClass> yourList = new Gson().fromJson(yourJson, listType);
关于java - 使用 Gson get() 从 Json 获取值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57693363/