[英]Java GSON simple write Json To string
im looking to find out how to make a SIMPLE JSON string using GSON. 我正在寻找如何使用GSON制作简单的JSON字符串。 All of the example require writing the output to a file, I just want to put it into a string.
所有示例都需要将输出写入文件,我只想将其放入字符串中。
I want to create a JSON string like this: 我想创建一个像这样的JSON字符串:
{"status":"success","error":"FULL ERROR"}
I don't see what the issue is 我看不出问题是什么
public static void main(String[] args) throws Exception {
CustomBean customBean = new CustomBean();
customBean.error = "FULL ERROR";
customBean.status = "success";
Gson gson = new Gson();
System.out.println(gson.toJson(customBean));
}
public static class CustomBean {
private String status;
private String error;
}
prints 版画
{"status":"success","error":"FULL ERROR"}
I think you should go through the Gson
javadoc. 我认为您应该阅读
Gson
javadoc。
If you don't want the new class, you can do 如果您不想上新课,可以
JsonObject object = new JsonObject();
object.addProperty("status", "success");
object.addProperty("error", "FULL ERROR");
System.out.println(object);
which also prints 也可以打印
{"status":"success","error":"FULL ERROR"}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.