2016-06-28 38 views
-3

任何人都可以請解釋如何將下面的json數據轉換爲python中的字符串。這是非常大的,但我需要你的幫助...... 您可以通過以下鏈接看到: - http://api.openweathermap.org/data/2.5/forecast/daily?q=delhi&mode=json&units=metric&cnt=7&appid=146f5f89c18a703450d3bd6737d4fc94 請表明,它的解決方案是爲我的項目的重要:-)將數據從Json轉換爲python中的字符串

+0

json已經是一個字符串... –

+0

' json'模塊包含'dumps'和'loads'的方法,它們從本地對象(字典,列表等等)來回執行序列化,然後傳輸到其他局域網軌距。 –

+0

最好展示你已經嘗試過的東西,並解釋你的目標是什麼。可能會有更好的解決方案,而不是像你想的那樣去做,而環境可以幫助人們思考它。 – Jeff

回答

0
import requests 

url = 'http://api.openweathermap.org/data/2.5/forecast/daily?q=delhi&mode=json&units=metric&cnt=7&appid=146f5f89c18a703450d3bd6737d4fc94' 

response = requests.get(url) 

response.text # this is a string 
response.json() # this is a json dictionary 

s = "The City is {city[name]} todays HIGH is {list[0][temp][max]}".format(**response.json()) 
print s 
+0

謝謝先生.... –

0

一些簡單的代碼,會從你的頁面讀取JSON併產生一個Python解釋如下。我已經使用了相鄰字符串的隱式連接來改進代碼的佈局。

import json 
import urllib.request 
f = urllib.request.urlopen 
     (url="http://api.openweathermap.org/data/2.5/forecast/daily?" 
      "q=delhi&mode=json&units=metric&" 
      "cnt=7&appid=146f5f89c18a703450d3bd6737d4fc94") 
content = f.read() 
result = json.loads(content.decode("utf-8")) 
print(result) 

這給了我下面的輸出(我還沒有在代碼風格顯示,因爲它會出現在一個長行):

{「城市」:{「座標」:{「LAT ':28.666668,'lon':77.216667},'country':'IN','id':1273294,'population':0,'name':'Delhi'},'cnt':7,'message': 0.0081,'list':[{'dt':1467093600,'weather':[{'icon':'01n','id':800,'description':'clear sky','main':'Clear' '溼度':82,'雲':0,'壓力':987.37,'速度':2.63,'temp':{'max':32,'eve':32,'night':30.67, 'min':30.67,'day':32''morn':32},'deg':104},{'dt':1467180000,'weather':[{'icon':'10d','id' :501,'description':'中雨','main':'Rain'}],'humidit y':74,'雲':12,'壓力':989.2,'速度':4.17,'雨':9.91,'temp':{'max':36.62,'eve':36.03,'night': 31.08,'min':29.39,'day':35.61,'morn':29.39},'deg':126},{'dt':1467266400,'weather':[{'icon':'02d' '801','description':'少雲','主':'雲'}],'溼度':71,'雲':12,'壓力':986.56,'速度':3.91,'temp ':{'max':36.27,'eve':35.19,'night':30.87,'min':29.04,'day':35.46,'morn':29.04},'deg':109',{'dt ':1467352800,'weather':[{'icon':'10d','id':502,'description':'heavy intensity rain','main':'Rain'}],'humidity':100, '雲':48,'壓力':984.48,'速度':0,'雨':18.47,'temp':{'max':30.87,'eve':30.87,'night':28.24,'min' :24.96,'day':27.16,'morn':24.96},'deg':0},{'dt':1467439200,'weather':[{'icon':'10d','id' '描述':'中雨','主':'雨'}],'溼度':0,'雲':17,'壓力':983.1,'速度':6.54,'雨':5.31' temp':{'max':35.48,'eve':32.96,'night':27.82, 'min':27.82,'day':35.48,'morn':29.83},'deg':121},{'dt':1467525600,'weather':[{'icon':'10d',' :501,'description':'中雨','main':'Rain'}],'humidity':0,'clouds':19,'pressure':984.27,'speed':3.17,'rain': 7.54,'temp':{'max':34.11,'eve':34.11,'night':27.88,'min':27.53,'day':33.77,'morn':27.53},'deg':133} ,'''','','''','''','''','''''''','''''''''''','''''''','''''', ':0,'雲':60,'壓力':984.82,'速度':5.28,'雨':54.7,'temp':{'max':33.12,'eve':33.12,'night':26.15 ,'min':25.78,'day':31.91,'morn':25.78},'deg':88}],'cod':'200'}

+0

謝謝先生:-) –

+0

很高興。既然它看起來可以幫助你,你可能想要考慮將它標記爲已接受,或者對其進行提升 – holdenweb