【问题标题】:Retrieve Daily Rate - Amadeus Car Rental API检索每日费率 - Amadeus Car Rental API
【发布时间】:2018-11-04 13:28:32
【问题描述】:

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

【问题讨论】:

    标签: python json python-3.x api amadeus


    【解决方案1】:

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

    daily_price = json_data["results"][z]["cars"][0]["rates"][0]["price"]["amount"]
    

    【讨论】:

    • 刚刚试了,还是一样的错误。 [0] 表示 Rate 是字典还是价格?也感谢您修复帖子中的代码。
    • 现在尝试回答@Brent
    • 这表明汽车也是一个包含单个字典@Brent的列表
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-09-14
    • 1970-01-01
    • 1970-01-01
    • 2022-10-20
    • 1970-01-01
    • 2021-04-08
    相关资源
    最近更新 更多