A
A
angernicky2020-08-20 12:28:56
Python
angernicky, 2020-08-20 12:28:56

How to decode json containing cyrillic?

Hello. I use sockets and send and receive packets through json serialization. But as soon as Cyrillic enters json, then complete gibberish is displayed on the screen in the form: \xd0\x9f\xd0\
How can I decode json so that all characters are displayed correctly?

# Отправка пакетов
def send_packages(conn, command):
    json_data = json.dumps(str(command))
    conn.send(bytes(json_data, 'utf-8'))

# Прием пакетов
def recieving_packages(conn):
    json_data = ''
    while True:
        try:
            data = conn.recv(8)
            json_data += data.decode('utf-8')
            return json.loads(json_data)
        except ValueError:
            continue

Answer the question

In order to leave comments, you need to log in

3 answer(s)
S
Sergey Gornostaev, 2020-08-20
@sergey-gornostaev

How to decode strange text from a page?

S
soremix, 2020-08-20
@SoreMix

Strange use of json.dumps(). It takes a json object and returns a string. You are passing a string. Although, it may be necessary .
For json.dumps() there is a parameter ensure_ascii , which is responsible for encoding. Try passing ensure_ascii=False
There is an encoding parameter for json.loads()

S
Sergey Svetlov, 2020-08-20
@svetloffs

import json
import codecs
json.load(codecs.open('sample.json', 'r', 'utf-8-sig'))

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question