我无法将json响应附加到列表中。我敢肯定这是一个简单的错误,但是已经花了数小时试图找出答案。 。 。任何建议将不胜感激。
edd_areas = ["San Francisco-Redwood City-South San Francisco MD",
"Los Angeles-Long Beach-Glendale MD",
"Oakland-Hayward-Berkeley MD",
"California"]
edd_year = []
edd_url = "https://data.edd.ca.gov/resource/4ezp-3bs3.json?"
for area in edd_areas:
edd_query_url = edd_url + "area_name=" + area + "&seasonally_adjusted_y_n=N"
edd_qresponse = requests.get(edd_query_url).json
edd_year.append(edd_qresponse["year"])
Error:
TypeError Traceback (most recent call last)
<ipython-input-10-f3a906010984> in <module>()
17
18
---> 19 edd_year.append(edd_qresponse["year"])
TypeError: 'method' object is not subscriptable
答案 0 :(得分:0)
两件事:
第一个问题在requests.get(edd_query_url).json
中。 .json
是一种方法,不返回json数据,您可能正尝试执行.json()
并实际上获取数据。
第二个问题是,实际的json数据是一个列表,因此您无法通过“ year”之类的字符串对其进行索引。
将所有内容放在一起:
edd_areas = ["San Francisco-Redwood City-South San Francisco MD",
"Los Angeles-Long Beach-Glendale MD",
"Oakland-Hayward-Berkeley MD",
"California"]
edd_year = []
edd_url = "https://data.edd.ca.gov/resource/4ezp-3bs3.json?"
for area in edd_areas:
edd_query_url = edd_url + "area_name=" + area + "&seasonally_adjusted_y_n=N"
edd_qresponse = requests.get(edd_query_url).json() # call the function!
edd_year.append(edd_qresponse) # just append the list since you can't index by "year"
您会看到类似以下的内容:
[[{'area_name': 'San Francisco-Redwood City-South San Francisco MD', 'area_type': 'Metropolitan Area', 'date': '1990-01-01T00:00:00.000' ...