简体   繁体   English

尝试遍历JSON中的数据

[英]Trying to iterate through data in JSON

I have a REST API for all police reports coming in, and I'm trying to iterate through the result of the GET request. 我有一个REST API,可以处理所有传入的警察报告,并且我正在尝试遍历GET请求的结果。

import requests
import json
import time

query = "http://gismaps.wichita.gov/agsweb/rest/services/COWGIS/WPD_Public/MapServer/0/query?where=1%3D1&outFields=CATEGORY,ADDRESS,PBADDRESS,CASEADDRESS,CITY,STARTDATE,DOW,INCMONTH,MONTHHS,OFFENSE_CO,OFFENSE_DE,PLACE_DESC,CLASS_TYPE,Shape,INCREPORT,STARTDATET,STARTTIME&outSR=4326&f=json"
query_value = requests.get(query)
crime = query_value.json()

def search(num):
    for i in range(1, num):
        print(crime["features"][i]["attributes"]["CATEGORY"])


search(10)

This code returns this data: 此代码返回以下数据:

AGG_ASSAULT DISORDERLY VANDALISM DISORDERLY DRUGS DRUGS OTHERTRAF_VIO OTHERTRAF_VIO LARCENY

But when I run this code: 但是当我运行这段代码时:

import requests
import json
import time

query = "http://gismaps.wichita.gov/agsweb/rest/services/COWGIS/WPD_Public/MapServer/0/query?where=1%3D1&outFields=CATEGORY,ADDRESS,PBADDRESS,CASEADDRESS,CITY,STARTDATE,DOW,INCMONTH,MONTHHS,OFFENSE_CO,OFFENSE_DE,PLACE_DESC,CLASS_TYPE,Shape,INCREPORT,STARTDATET,STARTTIME&outSR=4326&f=json"
query_value = requests.get(query)
crime = query_value.json()

def search(num):
    amt = 0
    for i in range(1, num):
        #print(crime["features"][i]["attributes"]["CATEGORY"])
        value = crime["features"][i]["attributes"]["CATEGORY"]
        print(str(value))
        if str(value) == "DRUGS":
            amt + 1
    print(amt, "matches")


search(10)

It returns "0 matches", even though it clearly outputs that there are 2 reports that have a value of "DRUGS", and it's returning me 0. Any help would be appreciated 它返回“ 0个匹配项”,即使它清楚地输出有2个报告的值都为“ DRUGS”,也返回0。任何帮助将不胜感激

The line amt + 1 doesn't do much. amt + 1行的作用不大。 To increment the counter, use amt += 1 . 要增加计数器,请使用amt += 1

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM