简体   繁体   中英

json.decoder.JSONDecodeError: Expecting ',' delimiter: Error in python

I send json data with socket in python

{"receiver": "2", "sender:": 1, "seq_num": 10, "data": "{"iv": "jdjhvwGriJ95kZwgDWlShw==", "ciphertext": "Fg7ugYYAnPzL+lG8d7QDDA=="}"}

Here is the data that I send. And it is string type, because I couldn't use sendall for json type.

And when I receive it, I tried to make that string into json. So I did,

            data = client_socket.recv(1024)
            #data = json.loads(data)
            data = json.loads(json.dumps(data))

I got json.decoder.JSONDecodeError: Expecting ',' delimiter: line 1 column 59 (char 58) error.

Please help!

There's a syntax error in your data field. By putting it in as "{"iv": "jdjhvwGriJ95kZwgDWlShw==", "ciphertext": "Fg7ugYYAnPzL+lG8d7QDDA=="}" , the double quotes mess up the encoder, causing it to throw an error.

If you wanted to value to be an object, you can simply remove the enclosing quotes like so:

data = {"receiver": "2", "sender:": 1, "seq_num": 10, "data": {"iv": "jdjhvwGriJ95kZwgDWlShw==", "ciphertext": "Fg7ugYYAnPzL+lG8d7QDDA=="}}

However, if you want it to be a string, then you have to make all of the double quotes inside escaped like this :

{
    "receiver": "2", "sender:": 1, "seq_num": 10, 
    "data": "{\"iv\": \"jdjhvwGriJ95kZwgDWlShw==\", \"ciphertext\": \"Fg7ugYYAnPzL+lG8d7QDDA==\"}"
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM