json.decoder.JSONDecodeError:期望值:第1行第1列(字符0)?

时间:2018-03-01 15:07:08

标签: json python-3.x websocket

test.py:

from hello import Put

        put_response = Put.put_device_save()
        rest_api_response = json.loads(put_response)['Device']['Name']['Value']
        socket_resp = json.loads(SocketIO.ssc('{"device":{"name":":null}}', "123.456.57.186"))['device']['name']

hello.py

class Put():     
     def put_device_save(self, device_instance_id, url):
            payload = [{"InstanceId": 12, "Device":{"Name":{"Value":"test"}}}]
            self.connect_ssc("http://localhost:1010/api/devices/save", payload)
            response = self.connect_ssc("GET", "http://localhost:1010/api/devices/all")
            return response

connect_ssc 功能:

def connect_ssc(self, method, url, body=None):

    token = self.generate_token()
    conn = http.client.HTTPConnection("localhost:1010")

    headers = {
        'accept': "application/json",
        'authorization': token,
        'content-type': "application/json",
        'cache-control': "no-cache",
    }
    time.sleep(5)
    # conn.request("GET", "/api/devices/all", payload, headers)
    conn.request(method, url, body, headers)
    try:
        res = conn.getresponse()
        data = res.read().decode("utf-8")
        return data
    except Exception as e:
        assert False, str(e)


token_used_counter = 0
token_starter = datetime.now()
expiry = token_starter + timedelta(minutes=30)

ssc 功能:

def ssc(command, ip, port=6970, timeout=1, buffer=1024):

    if isinstance(ip, str):
        ip_list = [ip]
    elif isinstance(ip, (list, tuple)):
        ip_list = ip
    else:
        raise TypeError('IPs must be strings or list/tuple of strings!')

    # Open Socket
    try:
        s = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
        s.settimeout(timeout)
        reply = None
        retry_count = 0
        for current_ip in ip_list:
            while reply is None and retry_count < 3:
                try:
                    s.sendto(command.encode('utf-8'), (current_ip, port))
                except:
                    print('Could not send {} to {} on Port {}.'.format(repr(command), current_ip, port))
                else:
                    print('Sent: SSC command {} to {} on Port {}.'.format(repr(command), current_ip, port))
                    try:
                        d = s.recvfrom(buffer)
                    except socket.error:
                        print('No Reply from {}'.format(current_ip))
                    else:
                        reply = d[0].decode('utf-8')
                        ip_reply = d[1][0]
                        port_reply = d[1][1]
                        print('Received: {} from {} on port {}'.format(repr(reply), ip_reply, port_reply))
                    retry_count += 1

        return str(reply)
    finally:
        s.close()

Stackrace错误:

C:\Python36\lib\json\__init__.py:354: in loads
    return _default_decoder.decode(s)
C:\Python36\lib\json\decoder.py:339: in decode
    obj, end = self.raw_decode(s, idx=_w(s, 0).end())
_ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _

self = <json.decoder.JSONDecoder object at 0x0000027AB5B27A20>, s = 'None'
idx = 0

    def raw_decode(self, s, idx=0):
        """Decode a JSON document from ``s`` (a ``str`` beginning with
            a JSON document) and return a 2-tuple of the Python
            representation and the index in ``s`` where the document ended.

            This can be used to decode a JSON document from a string that may
            have extraneous data at the end.

            """
        try:
            obj, end = self.scan_once(s, idx)
        except StopIteration as err:
>           raise JSONDecodeError("Expecting value", s, err.value) from None
E           json.decoder.JSONDecodeError: Expecting value: line 1 column 1 (char 0)

C:\Python36\lib\json\decoder.py:357: JSONDecodeError

我从 socket_resp = json.loads收到错误(SocketIO.ssc(&#39; {&#34; device&#34;:{&#34; name&#34;:&## 34;:null}}&#39;,&#34; 123.456.57.1​​86&#34;))[&#39;设备&#39;] [&#39;名称&#39;] 此行我觉得。 ssc功能创建问题我认为......请帮我解决这个问题。

0 个答案:

没有答案