JSONDe codeError:期待值:1行1列(CHAR 0) [英] JSONDecodeError: Expecting value: line 1 column 1 (char 0)

查看:15896
本文介绍了JSONDe codeError:期待值:1行1列(CHAR 0)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我收到错误期待值:1行1列(CHAR 0)试图去code JSON时

我使用的API调用的URL在浏览器工作正常,但通过一个卷曲的请求完成时给出了这样的错误。以下是code我用卷曲的请求。

错误发生在返回simplejson.loads(response_json)

  response_json = self.web_fetch(URL)
    response_json = response_json.de code(UTF-8)
    返回json.loads(response_json)
高清web_fetch(自我,网址):
        缓冲= StringIO的()
        卷曲= pycurl.Curl()
        curl.setopt(curl.URL,URL)
        curl.setopt(curl.TIMEOUT,self.timeout)
        curl.setopt(curl.WRITEFUNCTION,buffer.write)
        curl.perform()
        curl.close()
        响应= buffer.getvalue()条()
        返回响应

完整回溯:

回溯:

 在get_response文件/Users/nab/Desktop/myenv2/lib/python2.7/site-packages/django/core/handlers/base.py
  111.响应=回调(请求,* callback_args,** callback_kwargs)
文件/Users/nab/Desktop/pricestore/pricemodels/views.py在view_category
  620. apicall = api.API()。search_parts(CATEGORY_ID = STR(categoryofpart.api_id),制造商=制造商,过滤器=,=启动(catpage-1)* 20,上限= 20,sort_by = [[MPN ,ASC]]')
文件/Users/nab/Desktop/pricestore/pricemodels/api.py在search_parts
  176.回报simplejson.loads(response_json)
文件/Users/nab/Desktop/myenv2/lib/python2.7/site-packages/simplejson/__init__.py,在负荷
  455.回报_default_de coder.de code(S)
文件/Users/nab/Desktop/myenv2/lib/python2.7/site-packages/simplejson/de$c$cr.py在去code
  374. OBJ,结束= self.raw_de code(S)
文件/Users/nab/Desktop/myenv2/lib/python2.7/site-packages/simplejson/de$c$cr.py在raw_de code
  393.返回self.scan_once(S,IDX = _w(S,IDX).end关于())异常类型:在JSONDe codeError / pricemodels / 2 / DIR /
例外的值:期待值:1行1列(CHAR 0)


解决方案

要总结谈话中的注释:


  • 有没有必要使用 simplejson 库,相同的库包括在Python作为 JSON 模块。


  • 有没有必要去从UTF8到UNI code时, simplejson / JSON .loads()方法可以处理UTF8 EN codeD数据本身。


  • pycurl 有一个非常古老的API。除非你有特殊要求使用它,有更好的选择。


要求 提供最友好的API,包括JSON支持。如果可以,替换与您的来电:

 进口要求返回requests.get(URL)以.json()

I am getting error Expecting value: line 1 column 1 (char 0) when trying to decode JSON.

The URL I use for the API call works fine in the browser, but gives this error when done through a curl request. The following is the code I use for the curl request.

The error happens at return simplejson.loads(response_json)

    response_json = self.web_fetch(url)
    response_json = response_json.decode('utf-8')
    return json.loads(response_json)


def web_fetch(self, url):
        buffer = StringIO()
        curl = pycurl.Curl()
        curl.setopt(curl.URL, url)
        curl.setopt(curl.TIMEOUT, self.timeout)
        curl.setopt(curl.WRITEFUNCTION, buffer.write)
        curl.perform()
        curl.close()
        response = buffer.getvalue().strip()
        return response

Full Traceback:

Traceback:

File "/Users/nab/Desktop/myenv2/lib/python2.7/site-packages/django/core/handlers/base.py" in get_response
  111.                         response = callback(request, *callback_args, **callback_kwargs)
File "/Users/nab/Desktop/pricestore/pricemodels/views.py" in view_category
  620.     apicall=api.API().search_parts(category_id= str(categoryofpart.api_id), manufacturer = manufacturer, filter = filters, start=(catpage-1)*20, limit=20, sort_by='[["mpn","asc"]]')
File "/Users/nab/Desktop/pricestore/pricemodels/api.py" in search_parts
  176.         return simplejson.loads(response_json)
File "/Users/nab/Desktop/myenv2/lib/python2.7/site-packages/simplejson/__init__.py" in loads
  455.         return _default_decoder.decode(s)
File "/Users/nab/Desktop/myenv2/lib/python2.7/site-packages/simplejson/decoder.py" in decode
  374.         obj, end = self.raw_decode(s)
File "/Users/nab/Desktop/myenv2/lib/python2.7/site-packages/simplejson/decoder.py" in raw_decode
  393.         return self.scan_once(s, idx=_w(s, idx).end())

Exception Type: JSONDecodeError at /pricemodels/2/dir/
Exception Value: Expecting value: line 1 column 1 (char 0)

解决方案

To summarize the conversation in the comments:

  • There is no need to use simplejson library, the same library is included with Python as the json module.

  • There is no need to decode a response from UTF8 to unicode, the simplejson / json .loads() method can handle UTF8 encoded data natively.

  • pycurl has a very archaic API. Unless you have a specific requirement for using it, there are better choices.

requests offers the most friendly API, including JSON support. If you can, replace your call with:

import requests

return requests.get(url).json()

这篇关于JSONDe codeError:期待值:1行1列(CHAR 0)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆