解析json格式数据 获取自己需要的对象
获取指定对象的值
String result = "下面的json数据";
JSONObject jsonObject = JSONObject.fromObject(result);
// 取指定对象 filters
JSONArray filters = (JSONArray) jsonObject.get("filters");
// 取对应filters对象中的值 key - value 通过key 获取对应的value值
jsonObject1.get("key");
// 获取filters数组中包含filter为 1,2 的对象
String filterId = "1,2";
String[] filterId = filterIds.split(",");
JSONArray jsonArray = new JSONArray();
filters.forEach(item -> {
JSONObject jsonObject1 = JSONObject.fromObject(item);
for (String s : filterId) {
if (jsonObject1.get("filterId").toString().equals(s)) {
jsonArray.add(jsonObject1);
}
}
});
// 得到需要数据
return jsonArray;
#### json格式数组
// result 为json格式数组
/**
{
"groups": [
{
"groupId": 1,
"groupName": "Ad Blocking",
"displayNumber": 1
},
{
"groupId": 2,
"groupName": "Privacy",
"displayNumber": 2
},
{
"groupId": 3,
"groupName": "Social Widgets",
"displayNumber": 3
}
],
"tags": [
{
"tagId": 1,
"keyword": "purpose:ads"
},
{
"tagId": 2,
"keyword": "purpose:privacy"
},
{
"tagId": 3,
"keyword": "purpose:social"
}
],
"filters": [
{
"filterId": 101,
"name": "EasyList",
"description": "EasyList is the primary subscription that removes adverts from web pages in English. Already included in AdGuard Base filter.",
"timeAdded": "2014-06-30T07:56:55+0000",
"trustLevel": "low",
"version": "2.0.76.40",
"timeUpdated": "2021-08-19T17:00:25+0000",
"languages": [],
"tags": [
1
]
},
{
"filterId": 102,
"name": "ABPindo",
"description": "Indonesian supplement for EasyList.",
"timeAdded": "2014-06-30T07:56:55+0000",
"trustLevel": "high",
"version": "2.0.4.30",
"timeUpdated": "2021-08-02T14:40:21+0000",
"languages": [],
"tags": [
1,
9,
29
]
},
{
"filterId": 103,
"name": "Bulgarian list",
"description": "Bulgarian supplement for EasyList.",
"timeAdded": "2014-06-30T07:56:55+0000",
"trustLevel": "high",
"version": "2.0.0.32",
"timeUpdated": "2021-06-13T09:00:26+0000",
"languages": [
"bg"
],
"tags": [
1,
9,
10,
30
]
}
]
}
*/