如何在 PlayN 中将 POJO 转换为 JSON?

发布于 2024-12-29 09:13:06 字数 466 浏览 0 评论 0原文

我想将包含 json 内容的字符串发送到 REST 服务。如何将简单的 POJO 转换为 json 字符串?通常我会使用 Gson 来执行此操作:

我想要转换的类:

public class UserMdl {
   String name;
   String pwHash;
   //Constructur, getter and setter....
}

以及执行此操作的代码:

UserMdl userMd = new UserMdl("name", "pwHash");
Gson gson = new Gson();
String json = gson.toJson(userMdl);

但是我无法让 gson 在 HTML 构建中与 PlayN 一起使用。它仅适用于 Java 构建。我不知道如何使用 PlayN.json() 的东西来做到这一点。

I want to send a string with json content to a REST service. How can I convert a simple POJO to a json string? Normaly I would use Gson to do this:

My class that I want to convert:

public class UserMdl {
   String name;
   String pwHash;
   //Constructur, getter and setter....
}

And my code to do this:

UserMdl userMd = new UserMdl("name", "pwHash");
Gson gson = new Gson();
String json = gson.toJson(userMdl);

However I cannot get gson to work with PlayN in the HTML build. It only works in the Java build. And I cannot figure out how to do this with the PlayN.json() stuff.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文