Вот код, который я написал:
import json
n = int(input())
dict_ = {
для i в диапазоне (n):
ключ, значение = str(input()).split(' ')
dict_[ключ] = значение
sorted_by_keys = dict(sorted(dict_.items()))
json_object = json.dumps(sorted_by_keys, separators=(',' , ':'))
print(json_object)
Проблема:
Код: Выделить всё
• I am getting a RunError.JsonParseError when I try to run this code.
• The input format is a number of key-value pairs (e.g., 3 pairs), and the output should be a JSON string where the keys are sorted alphabetically.
Код: Выделить всё
• I’ve tried using separators=(',', ':') to remove extra spaces between keys and values, but it doesn’t seem to solve the problem.
• The program works for basic dictionary operations but seems to fail when converting it to a JSON string.
Код: Выделить всё
• Can anyone explain why I’m getting the JsonParseError?
• How can I fix this and ensure the output is a valid JSON string?
Я написал программу на Python, которая принимает несколько пар ключ-значение, сортирует ключи в алфавитном порядке и преобразует результат в строка JSON. Я использовал separators=(',', ':') для удаления лишних пробелов между ключами и значениями в выводе JSON.
Подробнее здесь: https://stackoverflow.com/questions/791 ... s-into-jso
Мобильная версия