2016-06-28 11 views
-3

誰でも、次のjsonデータをPythonの文字列に変換する方法を教えてください。それは非常に大きいですが、私はあなたの助けが必要... あなたは以下のリンクからそれを見ることができます: - http://api.openweathermap.org/data/2.5/forecast/daily?q=delhi&mode=json&units=metric&cnt=7&appid=146f5f89c18a703450d3bd6737d4fc94 それはあなたが好きなPythonでJSON文字列をデコードすることができますPythonでJsonからStringにデータを変換する

+0

jsonはすでに文字列です... –

+0

' json'モジュールには、ネイティブオブジェクト(dicts、リストなど)から他のlanへの送信用の文字列への逐次化を実行する 'dumps'と' loads'メソッドが含まれていますゲージ。 –

+0

あなたがすでに試したことを見せて、あなたの目標が何であるか説明してください。あなたが考えているやり方よりも優れた解決策があるかもしれません。そして、文脈は人々がそれについて考えるのを助けます。 – Jeff

答えて

0

:-)それは私のプロジェクトのために重要であるソリューションです示唆してくださいこの:

import json 
data = json.loads('json_string') 

出典:https://docs.python.org/2/library/json.html

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) 

これは私(それは単一の長い行に表示されるように私は、コードスタイルで示されていない)次のような出力が得られます。

{「都市」:{「COORD」:{ 'LAT '、' name ':' Delhi '}、' cnt ':7、' message ':'住所 ':' 'id':800、 'description': 'clear sky'、 'main': 'clear' }、 '湿度':82、 '雲':0、 '圧力':987.37、 'スピード':2.63、 '一時':{'最大':32、 'eve':32、 'night':30.67、 '天気':[{'アイコン:' 10d '、' id ':' min ':30.67、' day ':32、' morn ':32}、' deg ':104}、{' dt ':1467180000、' weather ' :501、 'description': 'moderate rain'、 'main': 'Rain'}]、 'humidit y ':74、' clouds ':12、' pressure ':989.2、' speed ':4.17、' rain ':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':[{'アイコン' 02d ' id ':801、' description ':' few clouds '、' main ':' clouds '}]、' humidity ':71、' clouds ':12、' pressure ':986.56、' speed ':3.91、' temp ':'最高 ':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 ' '雲':48、 '圧力':984.48、 'スピード':0、 'rain':18.47、 'temp':{'max':30.87、 'eve':30.87、 'night':28.24、 'min' :2496、 'day':27.16、 'morn':24.96}、 'deg':0}、{'dt':1467439200、 'weather':[{アイコン ':' 10d '、' id ':501、湿度:0、 '雲':17、 '圧力':983.1、 'スピード':6.54、 'rain':5.31、 'rain' temp ':{' max ':35.48、' eve ':32.96、' night ':27.82、 '分':27.82、 '日':35.48、 '朝:29.83}、' deg ':121}、{' dt ':1467525600、'天気 ':[{アイコン': '10d'、 ' : 'description': 'moderate rain'、 'main': 'Rain'}]、 'humidity':0、 'clouds':19、 'pressure':984.27、 'speed':3.17、 'rain': 7.5、 'temp':{'max':34.11、 'eve':34.11、 'night':27.88、 'min':27.53、 'day':33.77、 'morn':27.53}、 'deg':133} 、 '雨'、 '湿度'、 '湿度'、 '雨'、 '湿度': ' ':0、' clouds ':60、' pressure ':984.82、' speed ':5.28、' rain ':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

関連する問題