Getting TypeError: string indices must be integers
when trying to access a dictionary in python. I've tried using json.loads(r2)
thos produces "TypeError: the JSON object must be str, bytes or bytearray, not dict"
Not even sure if Im on the right track here, all I need to do is get the JSON which appears to be in one long string into something like x['SomeKey']
and the value at those keys using iteritems()
or similar?
r = requests.get(url, headers=headers)
r2 = r.json()
seq = json.dumps(r2)
print(seq['remotecontrol_id'])
r.json()
already returns a dictionary so I'm not sure why you passr2
tojson.dumps
that returns a string (hence thes
). The error message you received even suggests that ("TypeError: the JSON object must be str, bytes or bytearray, not dict")Your code should be
Ok, I got it figured out, sort of...
for i in r2:
print(r2['stuff'][0]['someval1']) print(r2['stuff'][1]['asomeval2'])
Anyway, hits works
Thanks for all the great advice!
The problem is that method
dumps
converts a python'sdictionary
into astring
, therefore in the lineseq['remote...']
you are accessing astring
. You should access the variabler2
instead ofseq