开发者

Problems loading JSON code in Python

I've been tyring to figure out how to load JSON objects in Python.

I'm able to send a JSON string to the server, but there it fails.

This is what I'm sending through a websocket with开发者_如何转开发 JavaScript:

ws.send('{"test":"test"}');

The server receives it without a problem, but can't validate it:

{"test":"test"}
This is not a JSON object!

Which comes forth from this code:

                    try:
                        data = data[:-1]
                        json.loads(data)
                    except ValueError:
                        print 'This is not a JSON object!'
                    else:
                        print ('JSON found!')

The data = data[:-1] is there to strip the delimiter sent through the websocket.


import traceback
try:
    d = json.loads(data[data.index('{'):-1])
except:
    traceback.print_exc()
else:
    print(d)

This way only the dictionary part of the data-string is parsed to json.loads().

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜