【问题标题】:What is the Python equivalent of StringEntity in Java?Java 中 StringEntity 的 Python 等价物是什么?
【发布时间】:2016-12-06 12:29:35
【问题描述】:

我在 Java 中有以下类:

class JsonEntity extends StringEntity {
    private final String string;

    public JsonEntity(String string, Charset charset) {
        super(string, ContentType.create(ContentType.APPLICATION_JSON.getMimeType(), charset));
        this.string = string
}

我不知道如何在 Python 2.7 中实现上述代码。有什么办法可以实现吗?

【问题讨论】:

    标签: java python json python-2.7


    【解决方案1】:

    我不知道我是否完全满足您的需求,但是如果您需要在 python 中使用特定字符集保存为 JSON,您可以执行以下操作:

    import json 
    json_string = json.dumps("your string even with wierd symbols", ensure_ascii=False).encode('utf8')
    

    注意:您可以将 utf8 更改为其他字符集。

    编辑: 无论如何,如果您需要扩展某些东西以实际实现您的 json 读取器/写入器,您可以扩展 JSONEncoder: https://docs.python.org/2.6/library/json.html#json.JSONEncoder

    【讨论】:

    • 这实际上不是我需要的,但感谢您提供的信息。
    猜你喜欢
    • 2011-04-13
    • 1970-01-01
    • 1970-01-01
    • 2011-01-04
    • 2011-04-12
    • 2016-10-30
    • 2011-01-04
    • 2017-08-31
    • 2013-10-18
    相关资源
    最近更新 更多