Python, Json, and String Indexes Must Be Integers, Not str

I am using Python and I sent a request to a url and got a response using httplib2. The answer I received was in JSon, how do I access a specific parameter. What I have at the moment:

resp, content = parser.request(access_token_uri, method = 'POST', body = params, headers =     headers)
raise Exception(content['access_token'])

and I get an error

string indices must be integers, not str

How should I do it?

thank

+5
source share
2 answers

Well, if the response type is json, and it comes in type str.

If you use 2.4 from Python, use simplejson, if 2.6 use json:

import json
# Your code
  retdict = json.loads(content)

Then treat it like a dictionary.

accesstoken = retdict['access_token']
+7
source

You can use a dump;

result = json.dumps(content)

result = json.loads(result)
0
source

All Articles