我有一個JSON如下面如何使用GSON從JSON樹
{
"apiVersion": "v1",
"metadata": {
"status": {
"statusCode": 0
},
},
"stuff": [
{
"name": {
"text": "red"
},
"properties": [
{
"attributes": {
"shade": "dark"
},
"component": {
"id": "BA1",
}
"type": "Color"
}
]
},
{
"name": {
"text": "Toyota Camry"
},
"properties": [
{
"attributes": {},
"component": {
"id": "MS",
},
"type": "Vehicle"
}
]
},
]
}
我使用GSON解析的結果是這樣獲得的JSON值:
Gson gson = new Gson();
JsonObject json = (JsonObject) gson.fromJson(in, JsonObject.class);
System.out.println(json.get("apiVersion").getAsString());
我可以得到apiVersion
,但不知道如何獲取json樹中的元素。例如,type
......如果我要輸出所有不同type
..in這種情況下Color and Vehicle
你的JSON字符串無效'}},'檢查的StatusCode後, – rajuGT
對不起,我只是用它作爲一個例子來傳達出點。 – Anthony