Unable to print wanted item from json api
<p>I'm trying to print the 'temp' item from a json urlfile. The problem is, I can't just type in <code>print(item['temp']</code>
If i do I get this error: </p>

<blockquote>
  <p>TypeError: string indices must be integers</p>
</blockquote>

<p>So I figured I had to use an integer instead, which is what I did. But when it
prints out the result I got this:</p>

<p>"f
c"</p>

<p>This doesn't make any sense, doesn't matter what number I type in I get a result in the form of 2 letters.</p>

<p>Here's the code:</p>

<pre><code>import urllib.request
import json

url = urllib.request.urlopen("http://www.myweather2.com/developer/forecast.ashx?uac=fRnJKj3Xpi&amp;output=json&amp;query=SW1")
readurl = url.read()
json_obj = str(readurl, "utf-8")
data = json.loads(json_obj)

for item in data['weather']:
    print(item[0])
</code></pre>

<p><code>data['weather']</code> is a <em>dictionary</em>, and looping directly over a dictionary produces the keys in that dictionary. Here each key is a string; either <code>'curren_weather'</code> or <code>'forecast'</code>. As such, <code>item[0]</code> takes the first letter of each of these keys, so you end up printing <code>f</code> and <code>c</code>:</p>

<pre><code>&gt;&gt;&gt; for item in data['weather']:
...     print(item)
...
curren_weather
forecast
&gt;&gt;&gt; for item in data['weather']:
...     print(item[0])
...
c
f
</code></pre>

<p>I take it you want to print the <code>'temp'</code> key of the list of dictionaries in the list referenced by <code>curren_weather</code> instead:</p>

<pre><code>for curr in data['weather']['curren_weather']:
    print(curr['temp'] + curr['temp_unit'])
</code></pre>

<p>I added the <code>temp_unit</code> value too; the above outputs:</p>

<pre><code>15c
</code></pre>

python json api python-3.x urllib