我有以下代码。
{
"id": 1,
"name": "A green door",
"price": 12.50,
"tags": ["home", "green"]
}
{
"id": 2,
"name": "A blue door",
"price": 10.50,
"tags": ["home", "blue"]
}
我希望能够只选择具有特定“id”的对象。
JSONObject json = new JSONObject(result);
String name = json.opString("name");
String price = json.optString("price");
String tags = json.optString("tags");
最佳答案
假设您的 JSON 数据更符合
{
"objects": [
{
"id": 1,
"name": "A green door",
"price": 12.5,
"tags": [
"home",
"green"
]
},
{
"id": 2,
"name": "A blue door",
"price": 10.5,
"tags": [
"home",
"blue"
]
}
]
}
可以使用jsonPath来获取,比如只有id == 2
$.objects[?(@.id=='2')]
关于java - 如何根据值在android中选择json对象,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25296697/