1
import urllib2
response = urllib2.urlopen('http://api.xyz.com')
html = response.read()

IN http://api.xyz.com

{
  "responseHeader":{
    "status":0,
    "QTime":18,
    "params":{
      "indent":"on",
      "q":"",
      "wt":"json",}},
  "response":{"numFound":7984,"start":0,"maxScore":1.0,"docs":[
      {
       "id":"21",
       "first_name":"anurag"
      },
      {
       "id":"31",
       "first_name":"abhishek"
      }
    ]
}

Problem is: this url will return json output. and i want to read that json file but it is showing error: string indices must be integers, not str.

1
  • 1
    Please include the code that throws the error, and the full traceback. Commented Feb 6, 2013 at 18:14

1 Answer 1

2

You need to convert the JSON response to a python structure with the json library (included with Python):

import json

import urllib2
response = urllib2.urlopen('http://api.xyz.com')
data = json.load(response)  # note, no `.read()`, the library handles that

print data['response']
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.