Python 如何将unicode字符串写入json?

Python 如何将unicode字符串写入json?,python,Python,我想将unicode字符串写入json,但它会写入excape两次。 我在第8行尝试了rf{chars}和chars,但这并不重要。而不是c={}.formathexindex+I[2:];unicode=rf\u{c},只需使用unicode=chridex+i 目标是让Python字符串成为希望编码为JSON的值的Python文本\u0001是Unicode字符1的JSON表示,而不是Python表示;同一字符的Python表示形式仅为“\x01”。在Python3中使用chr将直接跳转到您

我想将unicode字符串写入json,但它会写入excape两次。 我在第8行尝试了rf{chars}和chars,但这并不重要。

而不是c={}.formathexindex+I[2:];unicode=rf\u{c},只需使用unicode=chridex+i


目标是让Python字符串成为希望编码为JSON的值的Python文本\u0001是Unicode字符1的JSON表示,而不是Python表示;同一字符的Python表示形式仅为“\x01”。在Python3中使用chr将直接跳转到您需要的内容。

u值是编码为JSON后的样子。如果要通过转储传递它,它应该是该值的字面Python表示,而不是JSON编码的Python表示。具体描述一下我的意思:如果要将十六进制ASCII字符1编码为JSON,那么它是JSON.dumps'\x01',而不是JSON.dumpsr'\u0001'。您的输入应该是您想要转储的确切字符,而不是描述该字符的JSON字符串。谢谢Charles Duffy我解决了!伟大的也许可以使用“添加答案”按钮将您的解决方案添加为答案?
def write_json(type: str, file_dir: str, ascent: int, height: int, chars: str):
    with open('result.json', 'w', encoding='utf-8') as make_file:
        custom_font = dict()
        custom_font["type"] = type
        custom_font["file"] = file_dir
        custom_font["ascent"] = ascent
        custom_font["height"] = height
        custom_font["chars"] = rf"{chars}"
        json_data = custom_font

        json.dump(json_data, make_file, indent="\t")
    with open('result.json', 'r') as f:
        json_data = json.load(f)

    print(json.dumps(json_data, indent="\t"))


if __name__ == '__main__':
    index = 30000
    amount = 20

    c = "{}".format(hex(index))[2:]

    for i in range(amount):
        c = "{}".format(hex(index+i))[2:]
        unicode = rf"\u{c}"
        print(unicode)
        write_json("bitmap", "skill:font/assassin/blasting/1.png", 41, 160, unicode)