tags:

views:

82

answers:

1

I grab some data from a URL, and search online to find out the data is in in Jason data format, but when I tried to use simplejson.loads(data), it will raise exception.

First time deal with jason data, any suggestion how to decode the data? Thanks

================= result = simplejson.loads(data, encoding="utf-8") File "F:\My Documents\My Dropbox\StockDataDownloader\simplejson_init_.py", line 401, in loads return cls(encoding=encoding, **kw).decode(s) File "F:\My Documents\My Dropbox\StockDataDownloader\simplejson\decoder.py", line 402, in decode obj, end = self.raw_decode(s, idx=_w(s, 0).end()) File "F:\My Documents\My Dropbox\StockDataDownloader\simplejson\decoder.py", line 420, in raw_decode raise JSONDecodeError("No JSON object could be decoded", s, idx) simplejson.decoder.JSONDecodeError: No JSON object could be decoded: line 1 column 0 (char 0)

============================

data = "{identifier:'ID', label:'As at Wed 4 Aug 2010 05:05 PM',items:[{ID:0,N:'2ndChance',NC:'528',R:'NONE',I:'NONE',M:'-',LT:0.335,C:0.015,VL:51.000,BV:20.000,B:0.330,S:0.345,SV:20.000,O:0.335,H:0.335,L:0.335,V:17085.000,SC:'4',PV:0.320,P:4.6875,P_:'X',V_:''},{ID:1,N:'8Telecom',NC:'E25',R:'NONE',I:'NONE',M:'-',LT:0.190,C:0.000,VL:965.000,BV:1305.000,B:0.185,S:0.190,SV:641.000,O:0.185,H:0.190,L:0.185,V:179525.000,SC:'2',PV:0.190,P:0.0,P_:'X',V_:''},{ID:2,N:'A-Sonic',NC:'A53',R:'NONE',I:'NONE',M:'-',LT:0.090,C:0.005,VL:1278.000,BV:17.000,B:0.090,S:0.095,SV:346.000,O:0.090,H:0.090,L:0.090,V:115020.000,SC:'A',PV:0.085,P:5.882352734375,P_:'X',V_:''},{ID:3,N:'AA Grp',NC:'5GZ',R:'NONE',I:'NONE',M:'t',LT:0.000,C:0.000,VL:0.000,BV:100.000,B:0.050,S:0.060,SV:50.000,O:0.000,H:0.000,L:0.000,V:0.000,SC:'2',PV:0.050,P:0.0,P_:'X',V_:''}]}"

A: 

You're using simplejson correctly, but the site that gave you that data isn't using JSON format properly. Look at json.org, which uses simple syntax diagrams to show what is JSON: in the object diagram, after { (unless the object is empty, in which case a } immediately follows), JSON always has a string -- and as you see in that diagram, this means something that starts with a double quote. So, the very start of the string:

{identifier:

tells you that's incorrect JSON -- no double quotes around the word identifier.

Working around this problem is not as easy as recognizing it's there, but I wanted to reassure you, at least, about your code. Sigh it does seem that broken websites, such a great tradition in old HTML days, are with us to stay no matter how modern the technology they break is...:-(

Alex Martelli
Thanks for your answer. Yah, I tried to use double quote to surround the keys, and change single quote to double quotes, can use simplejson to decode.So the only way is to use regular expression to search for keys and surround with double quotes, then replace all single quotes with double quotes, and then use simplejson to decode the data?The data is from stock exchange website to display stock information.
yongzhy
@yongzhy, I would recommend pyparsing rather than a mess of regexes + simplejson, see http://pyparsing.wikispaces.com/ -- but the gist is definitely correct: you can't use simplejson to parse something that isn't json.
Alex Martelli