JSONDecodeError:期望值:第1行第1列(char 0) [英] JSONDecodeError: Expecting value: line 1 column 1 (char 0)

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

问题描述

我在尝试解码JSON时遇到错误期望值:第1行第1列(char 0)

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

用于API调用的URL在浏览器中正常工作,但在通过curl请求完成时会出现此错误。以下是我用于curl请求的代码。

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.

错误发生在 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

完全追踪:

追踪:

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)


推荐答案

在评论中总结会话:


  • 要使用 simplejson 库,Python中包含与 json 模块相同的库。

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

没有必要解码从UTF8到unicode的响应, simplejson / json

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

<$ c>

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

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

import requests

return requests.get(url).json()

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

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