0

我在从我们的 Amadeus Car Rental 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"
        }
      } ],
4

1 回答 1

1

你错过了其中一些是列表中的字典

daily_price = json_data["results"][z]["cars"][0]["rates"][0]["price"]["amount"]
于 2018-11-04T15:08:18.560 回答