在线观看不卡亚洲电影_亚洲妓女99综合网_91青青青亚洲娱乐在线观看_日韩无码高清综合久久

鍍金池/ 問答/Python/ Python中使用json.loads解碼字符串時(shí)出錯(cuò):

Python中使用json.loads解碼字符串時(shí)出錯(cuò):

1.報(bào)錯(cuò)信息:

File "C:\Users\lenovo\Desktop\client.py", line 90, in callback
    ret = json.loads(response.text)
  File "C:\Users\lenovo\AppData\Local\Programs\Python\Python36\lib\json\__init__.py", line 354, in loads
    return _default_decoder.decode(s)
  File "C:\Users\lenovo\AppData\Local\Programs\Python\Python36\lib\json\decoder.py", line 339, in decode
    obj, end = self.raw_decode(s, idx=_w(s, 0).end())
  File "C:\Users\lenovo\AppData\Local\Programs\Python\Python36\lib\json\decoder.py", line 357, in raw_decode
    raise JSONDecodeError("Expecting value", s, err.value) from None
json.decoder.JSONDecodeError: Expecting value: line 1 column 1 (char 0)

2.報(bào)錯(cuò)代碼:

#!/usr/bin/env python
# -*- coding:utf-8 -*-

import os
import json
import time
import hashlib
import requests
from src import plugins
from lib.serialize import Json
from lib.log import Logger
from config import settings

from concurrent.futures import ThreadPoolExecutor


class AutoBase(object):

    def __init__(self):
        self.asset_api = settings.ASSET_API
        self.key = settings.KEY
        self.key_name = settings.AUTH_KEY_NAME

    def auth_key(self):
        ha = hashlib.md5(self.key.encode('utf-8')) # 加鹽
        time_span = time.time()
        ha.update(bytes("%s|%f" % (self.key, time_span), encoding='utf-8'))
        encryption = ha.hexdigest()
        result = "%s|%f" % (encryption, time_span)
        # {auth-key: 173b81be05cd997eeac31e2fa99eff1c|1492395689.3360105
        return {self.key_name: result}

    def get_asset(self):
        try:
            headers = {}
            headers.update(self.auth_key())
            response = requests.get(
                url=self.asset_api,
                headers=headers
            )
        except Exception as e:
            response = e
        return response.json()

    def post_asset(self, msg, callback=None):
        status = True
        try:
            headers = {}
            headers.update(self.auth_key())
            response = requests.post(
                url=self.asset_api,
                headers=headers,
                json=msg
            )
        except Exception as e:
            response = e
            status = False
        if callback:
            callback(status, response)

    def process(self):
        raise NotImplementedError('you must implement process method')

    def callback(self, status, response):
        if not status:
            Logger().log(str(response), False)
            return
        ret = json.loads(response.text)
        if ret['code'] == 1000:
            Logger().log(ret['message'], True)
        else:
            Logger().log(ret['message'], False)
回答
編輯回答
好難瘦

json.loads(jsonstr)
這里面的參數(shù)只能是json格式的字符串,你可以看看response.text是不是字符串,是不是符合json格式,如果不是,就是response.content或者其它屬性才是請求返回的包體內(nèi)容。

2017年11月1日 06:25
編輯回答
冷溫柔

你先把你要loads的內(nèi)容打印出來, 放到https://www.json.cn/看看能不...
如果不能, 就是數(shù)據(jù)問題
如果可以, 那就是其他問題, 從你的問題看, 一般都是你的數(shù)據(jù)有問題。

2017年1月7日 03:50
編輯回答
遲月
json.decoder.JSONDecodeError: Expecting value: line 1 column 1 (char 0)

response.text 的第一個(gè)字符出錯(cuò),是不是第一個(gè)字符不是英文的 {,或者第一個(gè)字符是其他標(biāo)點(diǎn)符號

2017年4月2日 07:26