我在从Amadeus租车服务API返回的JSON文件中检索每日费率时遇到了麻烦。
我遇到了daily_price变量的代码错误(列表索引必须是整数或切片,而不是str)。
w, h = 3, i
final_list = [[0 for x in range(w)] for y in range(h)]
while z < i:
company_name = (json_data["results"][z]["provider"]["company_name"])
daily_price = (json_data["results"][z]["cars"]["rates"]["price"]["amount"])
#total_price = (json_data["results"][z]["cars"]["estimated_total"]["amount"])
final_list[z][0] = company_name
final_list[z][1] = daily_price
#final_list[z][1] = total_price
final_list[z][2] = z
z = z + 1
return HttpResponse(final_list)
当我没有正确浏览JSON结构时,我已经看到了这一点。我尝试了多种格式,但不确定是否要检索嵌套在JSON中的每日费率。
"results" : [ {
"provider" : {
"company_code" : "ZU",
"company_name" : "AUTO EUROPE"
},
"branch_id" : "SLCT01",
"location" : {
"latitude" : 40.78994,
"longitude" : -111.98125
},
"airport" : "SLC",
"address" : {
"line1" : "SALT LAKE CITY INTERNATIONAL AIRPORT",
"city" : "SALT LAKE CITY",
"country" : "US"
},
"cars" : [ {
"vehicle_info" : {
"acriss_code" : "ED",
"transmission" : "No information available",
"fuel" : "No information available",
"category" : "Economy",
"type" : "4-5 Door"
},
"rates" : [ {
"type" : "DAILY",
"price" : {
"amount" : "26.00",
"currency" : "USD"
}
} ],
答案 0 :(得分:1)
您错过了其中一些是列表中的字典
daily_price = json_data["results"][z]["cars"][0]["rates"][0]["price"]["amount"]