2017-02-26 42 views
-7

https://maps.googleapis.com/maps/api/directions/json?origin=Disneyland&destination=Universal+Studios+Hollywood4&key=AIzaSyDFZyV9E89I3L0Gt9A67DplLpZEq2F1G00解析JSON以獲得特定鍵和值

這是指向Google Directions API提供的JSON的鏈接。我想提取所有被稱爲'機動'的​​關鍵值對。我怎樣才能爲它編寫一個Python代碼?

+3

不幸的是,有人已經回覆了您的免費工作要求。然而,我想象4個降價和即將關閉將表明這種問題在這裏不受歡迎。我們需要海報在這裏努力。 – halfer

回答

0

你可以從Python中使用jsonrequests庫,並使用postman一個應用程序來幫助你看到的JSON/API的)。這些操作嵌套在routes - >legs - >steps內,因此您可以迭代json中位於data["routes"][0]["legs"][0]["steps"]處的對象的每個對象。這裏有一些你可以用來幫助你入門的工作代碼。充分利用它來滿足您的需求。

import json 
import requests 

url = 'https://maps.googleapis.com/maps/api/directions/json?origin=Disneyland&destination=Universal+Studios+Hollywood4&key=AIzaSyDFZyV9E89I3L0Gt9A67DplLpZEq2F1G00' 
data = requests.get(url).json() 


def get_maneuvers(): 
    "returns a list of maneuvers and displays each one in list" 
    steps = [] 
    for step in data["routes"][0]["legs"][0]["steps"]: 
    if "maneuver" in step: 
     print step["maneuver"] 
     steps.append(step["maneuver"]) 

    return steps 

get_maneuvers()