Java 手动创建 Json 字符串

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/20904344/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-13 05:11:41  来源:igfitidea点击:

Create Json String manually

javajsongson

提问by user1145874

I'm trying to create a Json structure manually in Java. In the end, I need the a string representation of my Json object. Since my project already has a dependency to GSON, I wanted to use this. But after several hours of trying and googling, I think, I completely misunderstand something.

我正在尝试在 Java 中手动创建一个 Json 结构。最后,我需要 Json 对象的字符串表示。由于我的项目已经依赖于 GSON,我想使用它。但是经过几个小时的尝试和谷歌搜索,我想,我完全误解了一些东西。

At the moment, I have the following (non-working) code:

目前,我有以下(非工作)代码:

JsonObject user_auth = new JsonObject();
user_auth.addProperty("user_name", username);
user_auth.addProperty("password", password);
JsonObject rest_data = new JsonObject();
Gson gson = new Gson();
rest_data.addProperty("user_auth", gson.toJson(user_auth));
rest_data.addProperty("application", APPLICATION_NAME);
String payload = gson.toJson(rest_data);

The problem I'm facing is that the "user_auth" element gets escaped quotes (\" instead of " when it is added to the outer element. How can I prevent this? I also tried to use

我面临的问题是“user_auth”元素被转义引号(\”而不是“当它添加到外部元素时。我怎样才能防止这种情况?我也尝试使用

rest_data.addProperty("user_auth", user_auth.toString());

but this did exactely the same.

但这完全一样。

Regards,

问候,

回答by fluminis

Writing the JSON manually is not a good solution.

手动编写 JSON 不是一个好的解决方案。

It seems that you encode user_auth twice.

您似乎对 user_auth 进行了两次编码。

Try this instead:

试试这个:

JsonObject user_auth = new JsonObject();
user_auth.addProperty("user_name", username);
user_auth.addProperty("password", password);

JsonObject rest_data = new JsonObject();
rest_data.addProperty("user_auth", user_auth);
rest_data.addProperty("application", APPLICATION_NAME);

Gson gson = new Gson();
String payload = gson.toJson(rest_data);

回答by Arun

If you dont wont to add external jar then, try this

如果你不想添加外部罐子,试试这个

return "{UPLOAD:"+upload+",UPLOADSTATUS:"+perUpload+",TRANSCODE:"+transcode+",TRANSCODESTATUS:"+transcodestatus+"}";

return "{UPLOAD:"+upload+",UPLOADSTATUS:"+perUpload+",TRANSCODE:"+transcode+",TRANSCODESTATUS:"+transcodestatus+"}";

回答by Andres Leon Rangel

Use the addProperty() for the user_auth and then for the rest_data you need the payload.

对 user_auth 使用 addProperty(),然后对你需要有效负载的 rest_data 使用 addProperty()。

String payloadJSON = gson.toJson(rest_data);

回答by Evgeny Lebedev

Another way is directly convert POJO into JSON.

另一种方式是直接将POJO转成JSON。

1. Create POJO object

1.创建POJO对象

i.e. user class:

即用户类:

class User {

    @JsonProperty("user_name") // this annotation helps you to specify
                               // property name in result json
    private String userName;

    @JsonProperty("first_name")
    private String firstName;

    // ...

    // getters and setters here, hashcode, equals, etc.

}

2. Convert POJO to JSON string

2. POJO转JSON字符串

for example:

例如:

User user = new User("superlogin", "superpass", ...);

String jsonString = new Gson().toJson(user);

Thats all :)

就这样 :)

I think it's more convenient way than build json structure manually.

我认为这比手动构建 json 结构更方便。