使用 Gson 创建特定的 JSON 字符串

发布于 2025-01-03 17:40:00 字数 2051 浏览 0 评论 0原文

当我使用 JSON-Lib 项目时,我可以使用命令创建

JSONObject jObject = new JSONObject();
jObject.accumulate("articles", list);
String json = jObject.toString();

以下输出:

{
    "articles": [
        {
            "amount": "50",
            "id": "1",
            "pct": "50,00",
            "price": "162,37",
            "startamount": "100",
            "stockvalue": "8118,45"
        },
        {
            "amount": "20",
            "id": "2",
            "pct": "20,00",
            "price": "164,83",
            "startamount": "100",
            "stockvalue": "3296,60"
        },
        {
            "amount": "20",
            "id": "3",
            "pct": "20,00",
            "price": "170,40",
            "startamount": "100",
            "stockvalue": "3408,00"
        },...
]
}

同时我使用 Gson 项目并与其绑定。 但是下面的代码:

Gson gson = new Gson();
String json = gson.toJson(list);

会给我这个输出:

[
    {
        "id": "1",
        "amount": "50",
        "startamount": "100",
        "pct": "50,00",
        "price": "162,37",
        "stockvalue": "8118,45"
    },
    {
        "id": "2",
        "amount": "20",
        "startamount": "100",
        "pct": "20,00",
        "price": "164,83",
        "stockvalue": "3296,60"
    },...]

The elements in my list are objects from my pojo article

public class Article implements IsSerializable {
    private String id;
        private String amount;
        private String startamount;
        private String pct;
        private String price;
        private String stockvalue;

        public Article(){

        }

        //Setter & Getter
}

How can I create with Gson a JSON string like the one JSON-Lib will create (JSONObject with JSONArray which inlcudes JSONObjects) 。 将来,JSON 字符串将/必须用更多数组进行扩展(不是文章,而是其他东西)。

我从 HttpServlet 检索生成的 JSON 字符串。将 Gson JSON 字符串反序列化回 ArrayList 是否有意义,还是应该使用 GWT JavaScript 覆盖类型?

谢谢& BR, 迈贝克斯

When I use JSON-Lib project, I can create with the command

JSONObject jObject = new JSONObject();
jObject.accumulate("articles", list);
String json = jObject.toString();

The following Output:

{
    "articles": [
        {
            "amount": "50",
            "id": "1",
            "pct": "50,00",
            "price": "162,37",
            "startamount": "100",
            "stockvalue": "8118,45"
        },
        {
            "amount": "20",
            "id": "2",
            "pct": "20,00",
            "price": "164,83",
            "startamount": "100",
            "stockvalue": "3296,60"
        },
        {
            "amount": "20",
            "id": "3",
            "pct": "20,00",
            "price": "170,40",
            "startamount": "100",
            "stockvalue": "3408,00"
        },...
]
}

Meanwhile I use the Gson project and are tied to it.
But the following code:

Gson gson = new Gson();
String json = gson.toJson(list);

will give me this output:

[
    {
        "id": "1",
        "amount": "50",
        "startamount": "100",
        "pct": "50,00",
        "price": "162,37",
        "stockvalue": "8118,45"
    },
    {
        "id": "2",
        "amount": "20",
        "startamount": "100",
        "pct": "20,00",
        "price": "164,83",
        "stockvalue": "3296,60"
    },...]

The elements in my list are objects from my pojo article

public class Article implements IsSerializable {
    private String id;
        private String amount;
        private String startamount;
        private String pct;
        private String price;
        private String stockvalue;

        public Article(){

        }

        //Setter & Getter
}

How can I create with the help of Gson a JSON string like the one JSON-Lib will create (JSONObject with JSONArray which inlcudes JSONObjects).
In future the JSON String will/must be extended with more arrays (not articles, but other stuff).

I retrieve this generated JSON String from a HttpServlet. Does it make sense to deserialize the Gson JSON String back to ArrayList or should I use the GWT JavaScript Overlay Types?

Thanks & BR,
mybecks

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

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

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

家住魔仙堡 2025-01-10 17:40:00

发生这种情况是因为您向 Gson 提供了一个对象列表。因此,如果您希望获得与第一个 json 片段相同的输出,则必须定义对象,该对象将保存文章列表。例如:

public class ArticlesResult {
   private List<Article> articles;


   ...//the rest of the code, like getters and setters
}

关于将 JsArray 转换回 ArrayList。这取决于情况,大多数时候最好使用覆盖类型,因为它们创建速度更快,并且在生成的 JS 代码中占用空间较小。但这仅适用于生产模式。在开发模式下,覆盖类型速度较慢,因此如果您快速访问覆盖类型,您将在那里浪费大量时间。

It happens because you give to the Gson a list of objects. So if you want to have the same output as in first json snippet, you have to define object, which will hold the list of articles. E.g. :

public class ArticlesResult {
   private List<Article> articles;


   ...//the rest of the code, like getters and setters
}

Regarding converting JsArray back to the ArrayList. It depends, most of the times it is better to use overlay types, since they are faster to create, and have small footprint in generated JS code. But it is true only for production mode. In devmode, overlay types are slower, so if you rapidly access overlay types, you will waste a lot of time there.

静水深流 2025-01-10 17:40:00

引用Gson 用户指南

Gson 是一个 Java 库,可用于将 Java 对象转换为其 JSON 表示形式。

这意味着您必须提供您想要在序列化为 JSON 的 Java 对象的 JSON 字符串中看到的完整结构。

在您的情况下,您需要将文章列表包装在具有“articles”属性的 bean 中。

如果需要构建动态结构,Gson 可能不是您的最佳工具。
您可以将 Gson 与 JAXB 等 XML 绑定工具进行比较。与 JAXB 一起使用的 bean 结构不应在运行时更改。

To quote the Gson User Guide:

Gson is a Java library that can be used to convert Java Objects into their JSON representation.

This means that you have to provide the complete structure that you want to see in the JSON string in the Java objects that you serialize into JSON.

In your case you need to wrap your list of articles in a bean that has a property "articles".

If it is required to build dynamic structures, Gson might not be the best tool for you.
you can compare Gson with XML binding tools like JAXB. A bean structure used with JAXB is not meant to be changed at runtime.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文