7

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

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

and I get the error

string indices must be integers, not str

How do I do it?

Thanks

Meir
  • 1,943
  • 5
  • 22
  • 38

2 Answers2

8

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

If you are running 2.4 of 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']
IainS
  • 260
  • 3
  • 14
0

You can use dump;

result = json.dumps(content)

result = json.loads(result)
code_ada
  • 874
  • 12
  • 25