2016-06-28 37 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