简体   繁体   English

如何通过具有多个过滤器的API查询JSON数据?

[英]How do I query JSON data from an API with multiple filters?

I'm retrieving data from a JSON API. 我正在从JSON API检索数据。
The data consists of a list of apps, each app having lots of properties. 数据由应用程序列表组成,每个应用程序都具有许多属性。
I only want the apps that match query_input and that are not trusted (see API documentation picture): 我只想要与query_input匹配且不受信任的应用程序(请参阅API文档图片): 在此处输入图片说明 I want the intersection of the two. 我想要两者的交集。 I only want apps that match both conditions. 我只想要同时符合这两个条件的应用程序。 If I use: 如果我使用:

parameters = {"query" : query_input, "trusted" : "false", "limit" : 3}
request = requests.get("http://(certain api link)", params = parameters)
print(json.dumps(request.json(), indent = 4))

If I use the code above, and set query_input as "facebook" I get this: 如果我使用上面的代码,并将query_input设置为“ facebook”, query_input得到以下信息:

{
    "info": {
        "status": "OK",
        "time": {
            "seconds": 0.068413019180298,
            "human": "68 milliseconds"
        }
    },
    "datalist": {
        "total": 1000,
        "count": 3,
        "offset": 0,
        "limit": 3,
        "next": 3,
        "hidden": 0,
        "loaded": true,
        "list": [
            {
                "id": 44893072,
                "name": "Facebook",
                "package": "com.facebook.katana",
                "uname": "facebook",
                "size": 56986654,
                "icon": "http://(certain link)",
                "graphic": "http://(certain link)",
                "added": "2013-02-15 03:30:51",
                "modified": "2019-03-21 02:24:03",
                "updated": "2019-03-21 23:01:10",
                "uptype": "xxxxxxxxx",
                "store": {
                    "id": 65518,
                    "name": "mark8",
                    "avatar": "http://(certain link)",
                    "appearance": {
                        "theme": "lightsky",
                        "description": ""
                    },
                    "stats": {
                        "apps": 3552,
                        "subscribers": 1110865,
                        "downloads": 505861392
                    }
                },
                "file": {
                    "vername": "215.0.0.0.37",
                    "vercode": 146691804,
                    "md5sum": "bc0ed01e35b7f888bdaa34eb2c2a2e1f",
                    "filesize": 56986654,
                    "path": "http://(certain link)",
                    "path_alt": "(certain link)",
                    "malware": {
                        "rank": "TRUSTED"
                    }
                },
                "stats": {
                    "downloads": 10272286,
                    "pdownloads": 122342832,
                    "rating": {
                        "avg": 4.2,
                        "total": 4966
                    },
                    "prating": {
                        "avg": 4.17,
                        "total": 20811
                    }
                },
                "has_versions": true,
                "obb": null,
                "appcoins": {
                    "advertising": false,
                    "billing": false
                }
            },
            {
                "id": 44918187,
                "name": "Facebook Lite",
                "package": "com.facebook.lite",
                "uname": "facebook-lite",
                "size": 1292422,
                "icon": "http://(certain link)",
                "graphic": "(certain link)",
                "added": "2017-09-21 11:39:42",
                "modified": "2019-03-21 19:06:13",
                "updated": "2019-03-21 23:04:44",
                "uptype": "xxxxxxx",
                "store": {
                    "id": 1618942,
                    "name": "hazel777",
                    "avatar": "http://(certain link)",
                    "appearance": {
                        "theme": "pink",
                        "description": "Thanks 4 being a follower"
                    },
                    "stats": {
                        "apps": 4098,
                        "subscribers": 63185,
                        "downloads": 28170613
                    }
                },
                "file": {
                    "vername": "139.0.0.9.85",
                    "vercode": 146791354,
                    "md5sum": "xxxxxxxxxxxxxxxxxxxxxxxx",
                    "filesize": 1292422,
                    "path": "http://(certain link)",
                    "path_alt": "http://(certain link)",
                    "malware": {
                        "rank": "TRUSTED"
                    }
                },
                "stats": {
                    "downloads": 18393,
                    "pdownloads": 23956738,
                    "rating": {
                        "avg": 3,
                        "total": 2
                    },
                    "prating": {
                        "avg": 4.28,
                        "total": 2225
                    }
                },
                "has_versions": true,
                "obb": null,
                "appcoins": {
                    "advertising": false,
                    "billing": false
                }
            },
            {
                "id": 3126316,
                "name": "Facebook",
                "package": "com.sonyericsson.androidapp.fbsetupwizard",
                "uname": "xperia-fb-setup",
                "size": 181391,
                "icon": "http://(certain link)",
                "graphic": null,
                "added": "2013-03-26 03:26:09",
                "modified": "2013-05-05 17:59:53",
                "updated": "2019-03-06 19:05:21",
                "uptype": "dropbox",
                "store": {
                    "id": 8655,
                    "name": "vitonline",
                    "avatar": "http://(certain link)",
                    "appearance": {
                        "theme": "light-blue",
                        "description": ""
                    },
                    "stats": {
                        "apps": 8609,
                        "subscribers": 27653,
                        "downloads": 3807977
                    }
                },
                "file": {
                    "vername": "5.0.A.0.10",
                    "vercode": 10485770,
                    "md5sum": "xxxxxxxxxxxxxxxx",
                    "filesize": 181391,
                    "path": "http://(certain link)",
                    "path_alt": "http://(certain link)",
                    "malware": {
                        "rank": "TRUSTED"
                    }
                },
                "stats": {
                    "downloads": 218559,
                    "pdownloads": 269179,
                    "rating": {
                        "avg": 3.75,
                        "total": 44
                    },
                    "prating": {
                        "avg": 3.63,
                        "total": 98
                    }
                },
                "has_versions": true,
                "obb": null,
                "appcoins": {
                    "advertising": false,
                    "billing": false
                }
            }
        ]
    }
}

It sort of works. 有点工作。
I get facebook related apps, and because "limit" : 3 , it only shows 3 apps (all apps are in "datalist" > "list", and each app begins with "id" and ends with "billing"). 我得到了与Facebook相关的应用程序,并且因为"limit" : 3 ,所以它仅显示3个应用程序(所有应用程序都在“ datalist”>“ list”中,每个应用程序均以“ id”开头并以“ billing”结束)。
I should only be getting apps with malware rankings of "UNKNOWN", but I'm getting apps with malware rankings of "TRUSTED", which I don't want. 我应该只获得恶意软件排名为“未知”的应用程序,但是我不想要恶意软件排名为“ TRUSTED”的应用程序。

API文档将false表示为trusted的JSON布尔值,因此在Python中,您应该使用布尔值False而不是字符串值"false" ,服务器将其视为真值而不是假值:

parameters = {"query" : query_input, "trusted" : False, "limit" : 3}

I think the documentation is a bit misleading. 我认为文档有些误导。 If you set your trusted parameter to true it will List trusted apps only . 如果将您的可信参数设置为true,它将List trusted apps only If you set it to false it will be all apps Trusted and Unknown. 如果将其设置为false,则所有应用程序均为Trusted and Unknown。

There is no option to get only unknown apps from a source. 无法选择仅从来源获取未知应用程序。

I've tried the query for Facebook and all apps return as Trusted. 我已经尝试过查询Facebook,并且所有应用都返回“受信任”状态。 Perhaps this is because there are no untrusted/unknown apps from that source. 可能是因为该来源中没有不可信/未知的应用。 Checking with terrarium as the query you can get some Unknown trust levels with trusted=false and then they are removed when trusted=true 使用terrarium作为查询,您可以使用trusted=false获得一些未知的信任级别,然后在trusted=true时将其删除

So it looks like you will manually need to get all the results and then filter your dictionary where the malware rank is not equal to Trusted 因此,您似乎需要手动获取所有结果,然后在恶意软件排名不等于“可信”的情况下过滤字典

app_data = request.json()
untrusted = [app_entry for app_entry in app_data['datalist']['list'] if app_entry['file']['malware']['rank'].upper() != 'TRUSTED']

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何进行具有多个过滤器的Django数据库查询? - How do I make a Django database query that has multiple filters? 如何查询Monson API的Json数据? -Python - How do I query Json data fron Monzo API? - Python 如何使用 api 和 for 循环组合来自 json 文件的多个数据帧? (Python) - How do I combine multiple dataframes from a json file using an api and a for loop? (python) 如何将多个API请求写入有效的JSON文件? - How do I write multiple API requests to a valid JSON file? 如何编写对字段长度进行过滤的gql查询? - How do I write a gql query that filters on the length of a field? 如何使用Django进行查询,但有更多的过滤器和更新? - How can I do a query using Django but with more filters and update? 如何将硬币市值 API 数据从 JSON 转换为 Pandas ZC699575A5E8AFD9E22A7ECC8CAB1? - How do I convert Coin Market Cap API data from JSON to Pandas Dataframe? 如何将多个警告过滤器传递给python -W? - How do I pass multiple warning filters to python -W? 如何使用 API 请求遍历并附加来自多个页面的数据? - How do I iterate through and append the data from multiple pages with an API request? 如何模拟查询 SQLAlchemy 中的连接表和 alchemy-mock where-statement 从多个表中过滤数据时的模拟? - How to mock query for joined tables in SQLAlchemy and alchemy-mock when where-statement filters data from multiple tables?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM