解析嵌套的JSON Python

时间:2016-03-22 01:30:00

标签: python json google-qpx-express-api

data = response.json()
sortJson = json.dumps(data, sort_keys=True,
                indent=2, separators=(',', ':'))

result = json.loads (data)
print ('"saleTotal":', result['trips']['tripOption']['pricing']['saleTotal'])

这是我目前的代码。我正在寻找解析嵌套的JSON文件,但每次运行时我都会收到以下错误:

TypeError:JSON对象必须是str,而不是'dict'

Pretty Printed的JSON文件如下所示:

 {
  "kind":"qpxExpress#tripsSearch",
  "trips":{
    "data":{
      "aircraft":[
        {
          "code":"321",
          "kind":"qpxexpress#aircraftData",
          "name":"Airbus A321"
        }
      ],
      "airport":[
        {
          "city":"ORL",
          "code":"MCO",
          "kind":"qpxexpress#airportData",
          "name":"Orlando International"
        },
        {
          "city":"CHI",
          "code":"ORD",
          "kind":"qpxexpress#airportData",
          "name":"Chicago O'Hare"
        }
      ],
      "carrier":[
        {
          "code":"F9",
          "kind":"qpxexpress#carrierData",
          "name":"Frontier Airlines, Inc."
        }
      ],
      "city":[
        {
          "code":"CHI",
          "kind":"qpxexpress#cityData",
          "name":"Chicago"
        },
        {
          "code":"ORL",
          "kind":"qpxexpress#cityData",
          "name":"Orlando"
        }
      ],
      "kind":"qpxexpress#data",
      "tax":[
        {
          "id":"ZP",
          "kind":"qpxexpress#taxData",
          "name":"US Flight Segment Tax"
        },
        {
          "id":"AY_001",
          "kind":"qpxexpress#taxData",
          "name":"US September 11th Security Fee"
        },
        {
          "id":"US_001",
          "kind":"qpxexpress#taxData",
          "name":"US Transportation Tax"
        },
        {
          "id":"XF",
          "kind":"qpxexpress#taxData",
          "name":"US Passenger Facility Charge"
        }
      ]
    },
    "kind":"qpxexpress#tripOptions",
    "requestId":"2z1TQ9iVMcSlUH8HW0O0eq",
    "tripOption":[
      {
        "id":"WQZ8ICu2L8RLqt1MyMNFAQ001",
        "kind":"qpxexpress#tripOption",
        "pricing":[
          {
            "baseFareTotal":"USD37.11",
            "fare":[
              {
                "basisCode":"Z00ZSS5",
                "carrier":"F9",
                "destination":"ORL",
                "id":"AR5um4n2cToXHml3a125O0CU7toTISvPQER/01Xhbf2E",
                "kind":"qpxexpress#fareInfo",
                "origin":"CHI"
              }
            ],
            "fareCalculation":"ORD F9 MCO Q9.29 Q4.65 23.17Z00ZSS5 USD 37.11 END ZP ORD XT 2.79US 4.00ZP 5.60AY 4.50XF ORD4.50",
            "kind":"qpxexpress#pricingInfo",
            "latestTicketingTime":"2016-03-22T00:24-04:00",
            "passengers":{
              "adultCount":1,
              "kind":"qpxexpress#passengerCounts"
            },
            "ptc":"ADT",
            "saleFareTotal":"USD37.11",
            "saleTaxTotal":"USD16.89",
            "saleTotal":"USD54.00",
            "segmentPricing":[
              {
                "fareId":"AR5um4n2cToXHml3a125O0CU7toTISvPQER/01Xhbf2E",
                "kind":"qpxexpress#segmentPricing",
                "segmentId":"GoIDkawPBE2TZk14"
              }
            ],
            "tax":[
              {
                "chargeType":"GOVERNMENT",
                "code":"US",
                "country":"US",
                "id":"US_001",
                "kind":"qpxexpress#taxInfo",
                "salePrice":"USD2.79"
              },
              {
                "chargeType":"GOVERNMENT",
                "code":"AY",
                "country":"US",
                "id":"AY_001",
                "kind":"qpxexpress#taxInfo",
                "salePrice":"USD5.60"
              },
              {
                "chargeType":"GOVERNMENT",
                "code":"XF",
                "country":"US",
                "id":"XF",
                "kind":"qpxexpress#taxInfo",
                "salePrice":"USD4.50"
              },
              {
                "chargeType":"GOVERNMENT",
                "code":"ZP",
                "country":"US",
                "id":"ZP",
                "kind":"qpxexpress#taxInfo",
                "salePrice":"USD4.00"
              }
            ]
          }
        ],
        "saleTotal":"USD54.00",
        "slice":[
          {
            "duration":167,
            "kind":"qpxexpress#sliceInfo",
            "segment":[
              {
                "bookingCode":"Z",
                "bookingCodeCount":9,
                "cabin":"COACH",
                "duration":167,
                "flight":{
                  "carrier":"F9",
                  "number":"1294"
                },
                "id":"GoIDkawPBE2TZk14",
                "kind":"qpxexpress#segmentInfo",
                "leg":[
                  {
                    "aircraft":"321",
                    "arrivalTime":"2016-05-11T09:42-04:00",
                    "departureTime":"2016-05-11T05:55-05:00",
                    "destination":"MCO",
                    "duration":167,
                    "id":"LQKIza3yQIpaLyDq",
                    "kind":"qpxexpress#legInfo",
                    "meal":"Food and Beverages for Purchase",
                    "mileage":1006,
                    "origin":"ORD",
                    "originTerminal":"3",
                    "secure":true
                  }
                ],
                "marriedSegmentGroup":"0"
              }
            ]
          }
        ]
      }
    ]
  }
}

最终我正在尝试找到saleTotal,并在其旁边打印相应的值: saleTotal:54.00

1 个答案:

答案 0 :(得分:2)

tripOptionpricing是对象列表,如果您考虑到它会起作用:

print ('"saleTotal":', result['trips']['tripOption'][0]['pricing'][0]['saleTotal'])

输出:

"saleTotal": USD54.00