This is the cred.json file I am trying to parse in my program.
{"accounts":[
{
"id":"1",
"username":"user1",
"password":"password1"
},
{
"id":"2",
"username":"user2",
"password":"password2"
}
]}
Here is the code I use for it. This works, but I know it isn't the best way to do it.
import json
with open('cred.json') as cred_file:
parsed_json = json.load(cred_file)
cred_file.close
for x in range(0,2):
user = parsed_json["accounts"][x]["username"]
password = parsed_json["accounts"][x]["password"]
print user, ":", password
I want to do the same thing, without specifying the range for the loop. When I try to do the same with iteritems() or get() it is giving me error saying unicode doesn't support those functions.
Please suggest me a better way to do this.