简体   繁体   English

如何使用simplejson解码以下数据?

[英]How to use simplejson to decode following data?

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. 我从URL中获取一些数据,然后在线搜索以发现Jason数据格式的数据,但是当我尝试使用simplejson.loads(data)时,它将引发异常。

First time deal with jason data, any suggestion how to decode the data? 第一次处理jason数据时,有什么建议如何对数据进行解码? 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) ================结果= simplejson.loads(数据,编码=“ utf-8”)文件“ F:\\ My Documents \\ My Dropbox \\ StockDataDownloader \\ simplejson__init __。py”加载中的第401行返回cls(encoding = encoding,** kw).decode文件“ F:\\ My Documents \\ My Dropbox \\ StockDataDownloader \\ simplejson \\ decoder.py”,第402行,在解码obj中,结束= raw.decode中的self.raw_decode(s,idx = _w(s,0).end())文件“ F:\\ My Documents \\ My Dropbox \\ StockDataDownloader \\ simplejson \\ decoder.py”,第420行,引发JSONDecodeError(“ No可以解码JSON对象“,s,idx)simplejson.decoder.JSONDecodeError:无法解码JSON对象:第1行第0列(字符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_:''}]}" 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-声波' ,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_: ''} ]}”

You're using simplejson correctly, but the site that gave you that data isn't using JSON format properly. 您正确使用了simplejson ,但是为您提供数据的站点未正确使用JSON格式。 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. 看一下json.org ,它使用简单的语法图显示什么是JSON:在对象图中, {之后(除非对象为空,在这种情况下立即是}紧随其后),JSON始终有一个字符串 -和您一样在图中看到,这意味着以双引号开头的内容。 So, the very start of the string: 因此,字符串的最开始:

{identifier:

tells you that's incorrect JSON -- no double quotes around the word identifier . 告诉您JSON不正确-单词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...:-( 令人遗憾的是,无论破解的技术多么现代化,破碎的网站似乎在过去的HTML时代都是如此伟大,它与我们同在。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM