[英]Getting a specific value inside a JSON array (Java)
I'm trying to get the value of a specific attribute in a JSON file but instead I get a row content of the array. 我正在尝试获取JSON文件中特定属性的值,但我却获得了数组的行内容。
For example that's my JSON file: 例如,这就是我的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" }
}
]
}
}
I want to get only the values of attribute "type3" But my following code gets me all of them. 我只想获取属性“ 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");
}
I tried several approaches but it seems I'm doing it wrong. 我尝试了几种方法,但看来我做错了。
I only want to get this : { "type": "contributor" }
我只想得到这个:{“ type”:“ contributor”}
Then get that value (roughly) like so 然后像这样大致获得该值
bindings.getJSONObject(0).getJSONObject("type3")
you can use JsonPath.read to get all Type3 values as list. 您可以使用JsonPath.read来获取所有Type3值作为列表。
List value = JsonPath.read(bindings, "..type3"); 列表值= JsonPath.read(bindings,“ ..type3”);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.