[英]Getting a specific value inside a JSON array (Java)
我正在嘗試獲取JSON文件中特定屬性的值,但我卻獲得了數組的行內容。
例如,這就是我的JSON文件:
{
"head": {
"vars": [ "type1" , "pred" , "type2" ]
} ,
"results": {
"bindings": [
{
"type1": { "type": "Collection" } ,
"type2": { "type": "has" } ,
"type3": { "type": "contributor" }
} ,
{
"type1": { "type": "Collection2" } ,
"type2": { "type": "has2" } ,
"type3": { "type": "contributor2" }
}
]
}
}
我只想獲取屬性“ type3”的值,但是我的以下代碼將我全部獲取。
JSONObject obj = new JSONObject(json);
JSONObject results = obj.getJSONObject("results");
JSONArray bindings = results.getJSONArray("bindings");
for (int i=0; i<bindings.length(); i++)
{
JSONObject x = bindings.getJSONObject(i);
x.getJSONObject("type3");
}
我嘗試了幾種方法,但看來我做錯了。
我只想得到這個:{“ type”:“ contributor”}
然后像這樣大致獲得該值
bindings.getJSONObject(0).getJSONObject("type3")
您可以使用JsonPath.read來獲取所有Type3值作為列表。
列表值= JsonPath.read(bindings,“ ..type3”);
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.