4

JSON:

data = {"result":[{"name":"Teddy","list":{"0":"24","1":"43","2":"56"}},
           {"name":"Barney","list":{"0":"24","1":"43","2":"56"}]}

Code:

i = 0
j = 0
for p in data['result']:
    print('Name: ' + p['name'])
    for v in p['list']:
        i += 1
        print("{0} : {1}".format(i,v[j]))
        j += 1

I am trying to access each value and print them out, but unfortunately, without any success, any help is appreciated.

I have seen: Loop through all nested dictionary values?

Rarblack
  • 4,559
  • 4
  • 22
  • 33
haxxir411
  • 79
  • 1
  • 1
  • 8

1 Answers1

3

From your attempt, seems that what you want to do is the following:

data = {"result":[
    {"name":"Teddy","list":{"0":"24","1":"43","2":"56"}},
    {"name":"Barney","list":{"0":"24","1":"43","2":"56"}}]}

for p in data['result']:
    print('Name: ' + p['name'])
    for k, v in p['list'].items():
        print("{0} : {1}".format(k,v))

Note that data is not a JSON object but a Python dictionary.

Output:

Name: Teddy
1 : 43
0 : 24
2 : 56
Name: Barney
1 : 43
0 : 24
2 : 56
ettanany
  • 19,038
  • 9
  • 47
  • 63