python json解析跳过错误_在Python中解析JSON时出现各种错误

Attempting to parse json from a url requiring login. Including all my code here as I'm not sure where the error is.

try: import simplejson as json

except ImportError: import json

import urllib2

username = 'user'

password = '1234'

url = "https://www.blah.com/someplace"

# set up the username/password/url request

password_mgr = urllib2.HTTPPasswordMgrWithDefaultRealm()

password_mgr.add_password(None, "https://www.blah.com", username, password)

handler = urllib2.HTTPBasicAuthHandler(password_mgr)

opener = urllib2.build_opener(handler)

urllib2.install_opener(opener)

request = urllib2.Request(url)

response = opener.open(request)

# option 1

json_object = json.loads(str(response))

#option 2

json_object = json.loads(response)

If I run the code with option 1 (commenting out option 2), I get this error:

Traceback (most recent call last):

File "jsontest.py", line 22, in

json_object = json.loads(str(request))

File "/usr/lib/python2.7/dist-packages/simplejson/__init__.py", line 413, in loads

return _default_decoder.decode(s)

File "/usr/lib/python2.7/dist-packages/simplejson/decoder.py", line 402, in decode

obj, end = self.raw_decode(s, idx=_w(s, 0).end())

File "/usr/lib/python2.7/dist-packages/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)

If I run option 2:

Traceback (most recent call last):

File "jsontest.py", line 23, in

json_object = json.loads(request)

File "/usr/lib/python2.7/dist-packages/simplejson/__init__.py", line 413, in loads

return _default_decoder.decode(s)

File "/usr/lib/python2.7/dist-packages/simplejson/decoder.py", line 402, in decode

obj, end = self.raw_decode(s, idx=_w(s, 0).end())

TypeError: expected string or buffer

My sample JSON is valid as far as I can tell:

{"set1":[{"data1":"411","data2":"2033","data3":"1","data4":"43968077","data5":"217","data6":"106828","data7":[]}],

"set2":{"data8":"411","data9":"2033","data10":"43968077","data11":"217223360","data12":"106828"}}

simplejson version = 2.3.2,

Python 2.7.3

Very new to all this so any pointers would be very helpful.

解决方案

You want to decode the response, not the request:

json_object = json.load(response)

The response is a file-like object, so you can use .load() to have the json library read it directly.

Alternatively (at the cost of some temporary memory use), use the .loads() function with the fully read response:

json_object = json.loads(response.read())

Note that python 2.7 already includes the simplejson library, renamed to json:

import json

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值