简体   繁体   English

Java中StringEntity的Python等价物是什么?

[英]What is the Python equivalent of StringEntity in Java?

I have the following class in Java: 我在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
}

I have no clue how to implement the above code in Python 2.7. 我不知道如何在Python 2.7中实现上述代码。 Is there any way I can achieve this? 有什么办法可以实现吗?

I don' know if I exactly get your need, however if you need to save as JSON with a particular charset in python you can do something like this: 我不知道我是否完全满足您的需求,但是如果您需要在python中使用特定字符集保存为JSON,您可以执行以下操作:

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

NB: you can change utf8 with other charsets. 注意:您可以使用其他字符集更改utf8。

EDIT: Anyway if you need to extend something to actually implement your json reader/writer you can extend JSONEncoder: https://docs.python.org/2.6/library/json.html#json.JSONEncoder 编辑:无论如何,如果你需要扩展一些东西来实际实现你的json阅读器/编写器,你可以扩展JSONEncoder: https ://docs.python.org/2.6/library/json.html#json.JSONEncoder

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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