Python JSON decoding

exiva picture exiva · Mar 19, 2011 · Viewed 9.8k times · Source

I'm having some trouble decoding this json in python.

From basehttpserver I'm getting back

[
 {
    "changed_aspect": "media", 
    "object": "geography", 
    "object_id": "1306", 
    "subscription_id": 1326, 
    "time": 1300570688
 }
]

which I'm putting into simplejsondecoder with

data = simplejson.loads(s)

but when I look at the length of data it's coming back with 1, not 5 for the json objects like I'm expecting.

Here's all of the code, incase the problem lies elsewhere.

class httpserver(BaseHTTPServer.BaseHTTPRequestHandler):
    def do_POST(self):
        self.data_string = self.rfile.read(int(self.headers['Content-Length']))
        self.send_response(200)
        self.end_headers()

        data = simplejson.loads(self.data_string)
        print len(data)
        return

Answer

Andrew Clark picture Andrew Clark · Mar 19, 2011

When you decode the JSON you get exactly what it looks like, a list containing a single item.

data[0] should be the dictionary you expected to see.