I want to loop over the content of a JSON file and print it to the console.
I think I did mix up something with lists.
This is what I tried to get all the team_name
elements
from urllib2 import urlopen import json url = 'http://openligadb-json.heroku.com/api/teams_by_league_saison?league_saison=2012&league_shortcut=bl1' response = urlopen(url) json_obj = json.load(response) for i in json_obj['team']: print i
And this is my JSON (simplified:)
{ "team": [ { "team_icon_url": "http://www.openligadb.de/images/teamicons/Hamburger_SV.gif", "team_id": "100", "team_name": "Hamburger SV" }, { "team_icon_url": "http://www.openligadb.de/images/teamicons/FC_Schalke_04.gif", "team_id": "9", "team_name": "FC Schalke 04" } ] }
(Full JSON output to be found here: Link)
And of course I get an error, that I should use integer input in [], not string, but I don't get how I could do that.
for i in json_obj['team']: TypeError: string indices must be integers, not str
Here is the response
:
http://openligadb-json.heroku.com/api/teams_by_league_saison?league_saison=2012&league_shortcut=bl1 <addinfourl at 139755086292608 whose fp = <socket._fileobject object at 0x7f1b446d33d0>>
What did I get wrong?
To loop through a JSON array with JavaScript, we can use a for of loop. to loop through the json array with a for of loop. We assign the entry being looped through to obj . Then we get the value of the id property of the object in the loop and log it.
Use Object. values() or Object. entries(). These will return an array which we can then iterate over. Note that the const [key, value] = entry; syntax is an example of array destructuring that was introduced to the language in ES2015.
1) Create a Maven project and add json dependency in POM. xml file. 2) Create a string of JSON data which we convert into JSON object to manipulate its data. 3) After that, we get the JSON Array from the JSON Object using getJSONArray() method and store it into a variable of type JSONArray.
getJSONObject("JObjects"); JSONArray getArray = getObject. getJSONArray("JArray1"); for(int i = 0; i < getArray. size(); i++) { JSONObject objects = getArray. getJSONArray(i); //Iterate through the elements of the array i. //Get thier value. //Get the value for the first element and the value for the last element. }
Actually, to query the team_name
, just add it in brackets to the last line. Apart from that, it seems to work on Python 2.7.3 on command line.
from urllib2 import urlopen import json url = 'http://openligadb-json.heroku.com/api/teams_by_league_saison?league_saison=2012&league_shortcut=bl1' response = urlopen(url) json_obj = json.load(response) for i in json_obj['team']: print i['team_name']
Try this :
import urllib, urllib2, json url = 'http://openligadb-json.heroku.com/api/teams_by_league_saison?league_saison=2012&league_shortcut=bl1' request = urllib2.Request(url) request.add_header('User-Agent','Mozilla/4.0 (compatible; MSIE 5.5; Windows NT)') request.add_header('Content-Type','application/json') response = urllib2.urlopen(request) json_object = json.load(response) #print json_object['results'] if json_object['team'] == []: print 'No Data!' else: for rows in json_object['team']: print 'Team ID:' + rows['team_id'] print 'Team Name:' + rows['team_name'] print 'Team URL:' + rows['team_icon_url']
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With