0

I am using

httplib.HTTPConnection(self._myurl)
conn.request("GET", "/")
data = conn.getresponse().read()

now this URL returns an python type arrays similar to the below:

[1,"apple",23,"good"]
[2,"grape",4,"bad"]

Now i am getting this result from the service as a string in data. How do i get this result parsed/encoded as array/list straight away without having to dissect it myself and create an array?

2 Answers 2

3

If the server is returning JSON (which it looks like it might be) it is a simple matter of:

import json

# ... snip ...

rehydrated_data = json.loads(data)
0

[updated]
Service was actually designed for stream rather than data in bulk. So that is why I was actually getting array of objects instead of array of array objects.

I handled it at javascript side finally using the following logic.

1. data = data.replace(']','],')
2. data = '[' + data.rstrip(',') + ']'
3. data = json.loads(data)

Reply to the above answer
Actually it is not, the json will be

{"key":[[...],[...]]}

I have tried the code and it fails with the following error.

...
File "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/json/__init__.py", line 326, in loads
    return _default_decoder.decode(s)
  File "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/json/decoder.py", line 369, in decode
    raise ValueError(errmsg("Extra data", s, end, len(s)))
ValueError: Extra data: line 2 column 1 - line 38 column 119 (char 25 - 4048)
2
  • JSON includes arrays, not just objects. Commented Jun 12, 2013 at 0:02
  • Yes, and an array would be [],[],[] NOT [][][]. Commented Aug 27, 2014 at 14:07

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.