简体   繁体   English

如何使用GSON命名JSON对象数组?

[英]How to name an array of JSON objects using GSON?

I'm using gson library and Mustach.js template engine. 我正在使用gson库和Mustach.js模板引擎。 I'm passing Collection of Products to jsp page. 我正在将产品集合传递到jsp页面。

Now i'm using the following code: 现在我正在使用以下代码:

Collection<Product> products = productDAO.findAll();
    Gson gson = new Gson();
    JsonElement element = gson.toJsonTree(products,
            new TypeToken<Collection<Product>>() {
            }.getType());
    JsonArray jsonArray = element.getAsJsonArray();
    HttpServletResponse response = ServletActionContext.getResponse();
    response.setContentType("application/json");
    response.setCharacterEncoding("UTF-8");
    PrintWriter writer = response.getWriter();
    writer.print(jsonArray);

it generates following json output: 它生成以下json输出:

[{"name":"Ut Nisi A PC","price":1133.43,"storeId":1,"id":2},
{"name":"Ipsum Dolor Sit Company","price":967.45,"storeId":1,"id":3},
{"name":"Ligula Limited","price":156.66,"storeId":1,"id":100}]

What I want is to name an array: 我想要的是命名数组:

 {
    "products": 
    [{"name":"Ut Nisi A PC","price":1133.43,"storeId":1,"id":2},
    {"name":"Ipsum Dolor Sit Company","price":967.45,"storeId":1,"id":3},
    {"name":"Ligula Limited","price":156.66,"storeId":1,"id":100}]}

So then in jsp for Mustache.js template I can refer to array like: 因此,在用于Mustache.js模板的jsp中,我可以像这样引用数组:

   <div id="container"></div> 

    <script id="products-template" type="text/mustache-template">
        {{#products}}
            <li>{{name}},{{price}}</li>
        {{/products}}
    </script>
    $.ajax({
            type : 'GET',
            dataType : 'json',
            url : 'product/upload_products',
            success : function(products) {
                var template = $('#products-template').html();
                var info = Mustache.render(template, products);
                $('#container').html(info);
            }
        })
    </script>

How using GSON library such output can be achieved ? 如何使用GSON库实现这样的输出?

Simply create a new JsonObject and add a field 只需创建一个新的JsonObject并添加一个字段

JsonObject object = new JsonObject();
object.add("products", jsonArray);

then write that object to the response. 然后将该对象写入响应。

Use JsonObject. 使用JsonObject。 So, 所以,

JsonObject jsonObject = new JsonObject();
jsonObject.add("products", jsonArray); // jsonObject has the add method not put.

The key of a Json Object is always a String but the value can be a String, a boolean, a number, a json array, .... Json对象的键始终是字符串,但是值可以是字符串,布尔值,数字,json数组等。

If you have an object that is an array you only need to pass it to a jsonObject in the value and create a String key. 如果您有一个数组对象,则只需将其传递给值中的jsonObject并创建一个String键。

Manage the jsonObject like an HashMap. 像HashMap一样管理jsonObject。 To do what you need make a put("products", yourJsonArrayObject) in a new Json Object. 要执行所需的操作,请在新的Json对象中放置put(“ products”,yourJsonArrayObject)。

JsonObject yourNewObject = new JsonObject();
yourNewObject.put("products", yourJsonArrayObject);

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

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