Searching Dictionary in list
<p>I am working on my first project with API's and I am having trouble accessing the data. I am working off an example that calls its data with this loop:</p>

<pre><code>for item in data['objects']:
     print item['name'], item['phone']
</code></pre>

<p>This works great for data stored as nested dictionaries (the outside being called objects, and the inside containing the data)</p>

<p>The issue I am having is my data is formatted with dictonaries inside of lists</p>

<pre><code>[
{
"key":"2014cama",
"website":"http://www.cvrobotics.org/frc/regional.html",
"official":true,
"end_date":"2014-03-09",
"name":"Central Valley Regional",
"short_name":"Central Valley",
"facebook_eid":null,
"event_district_string":null,
"venue_address":"Madera South High School\n705 W. Pecan Avenue\nMadera, CA 93637\nUSA",      
 "event_district":0,
"location":"Madera, CA, USA",
"event_code":"cama",
"year":2014,
"webcast":[],
"timezone":"America/Los_Angeles",
"alliances":[],
"event_type_string":"Regional",
"start_date":"2014-03-07",
"event_type":0
},'more data...']
</code></pre>

<p>so calling,</p>

<pre><code>for item in data['objects']:
     print item['name']
</code></pre>

<p>Won't work to pull the value stored in <code>name</code>.</p>

<p>Any help would be much appreciated.</p>

<p>Edit: The full Dataset I'm pulling (<a href="http://www.thebluealliance.com/api/v2/team/frc254/2014/events?X-TBA-App-Id=Peter_Hartnett:Scouting:v1" rel="nofollow">http://www.thebluealliance.com/api/v2/team/frc254/2014/events?X-TBA-App-Id=Peter_Hartnett:Scouting:v1</a>) </p>

<p>And the code I am running:</p>

<pre><code>import json,urllib2, TBA

team ='frc254'
year = '2014'



Url = 'http://www.thebluealliance.com/api/v2/team/'+team+'/'+year+'/events?X-                        TBA-App-Id=Peter_Hartnett:Scouting:v1'
data = TBA.GetData(Url)

for item in data:
     print item['name']
</code></pre>

<p>The TBA Class just imports the data and returns it.</p>

<p>Edit2:
Here is the TBA class that pulls the data, I can assure you it is identical to that found at the link above</p>

<pre><code>import urllib2,cookielib
content='none'


def GetData(Url):
    site= Url
    hdr = {'User-Agent': 'Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.11 (KHTML, like Gecko) Chrome/23.0.1271.64 Safari/537.11',
       'Accept': '    'text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8',
       'Accept-Charset': 'ISO-8859-1,utf-8;q=0.7,*;q=0.3',
       'Accept-Encoding': 'none',
       'Accept-Language': 'en-US,en;q=0.8',
       'Connection': 'keep-alive'}

    req = urllib2.Request(site, headers=hdr)

    try:
        page = urllib2.urlopen(req)
    except urllib2.HTTPError, e:
        print e.fp.read()

    content = page.read()
    return content
</code></pre>

<p>If I understood it correctly, your <code>data['objects']</code> is now an entry in a list, right?</p>

<p>So just iterate the list and your logic will remain the same</p>

<pre><code>for item in objects:
     print item['name'], item['phone']
</code></pre>

<p>being </p>

<pre><code>objects = [
{
"key":"2014cama",
"website":"http://www.cvrobotics.org/frc/regional.html",
"official":true,
"end_date":"2014-03-09",
"name":"Central Valley Regional",
"short_name":"Central Valley",
"facebook_eid":null,
"event_district_string":null,
"venue_address":"Madera South High School\n705 W. Pecan Avenue\nMadera, CA 93637\nUSA",      
 "event_district":0,
"location":"Madera, CA, USA",
"event_code":"cama",
"year":2014,
"webcast":[],
"timezone":"America/Los_Angeles",
"alliances":[],
"event_type_string":"Regional",
"start_date":"2014-03-07",
"event_type":0
},'more data...']
</code></pre>

<h3>Edit</h3>

<p>I get your problem now. Your object <code>data</code> is a <code>string</code> that represents a <code>JSONArray</code> . You should <strong><em>load</em></strong> that before iterating, in order to be able to work with that as a real <code>list</code>, like so:</p>

<pre><code>data = GetData(Url)

loaded_array = json.loads(data)

for item in loaded_array:
     print item['name']
</code></pre>

python list dictionary