简体   繁体   English

在Java中将Json数组“ [{}]”转换为“ []”

[英]Convert Json array “[{}]” to “[]” in Java

I want to convert my Blank Json array to the Null Json array. 我想将我的Blank Json数组转换为Null Json数组。

For ex., My Json array is like "[{}]" and if I got this array then automatically converts to "[]". 例如,我的Json数组就像“ [{}]”,如果我得到了这个数组,那么它将自动转换为“ []”。

My code is like as below : 我的代码如下所示:

JsonObject jo = FetchData.getAllItemsAvg(request.getParameter("where"), request.getParameter("lastNum"),request.getParameter("limitAvgNum"));
JsonArray ja = new JsonArray();
ja.add(jo); // Some times ja like "[{}]" .

Check if the object is empty before adding it to the array. 将对象添加到数组之前,请检查该对象是否为空。 (assuming you're using JsonObject ): (假设您正在使用JsonObject ):

JsonObject jo = FetchData.getAllItemsAvg(
                          request.getParameter("where"),
                          request.getParameter("lastNum"),
                          request.getParameter("limitAvgNum"));
JsonArray ja = new JsonArray();
if(!jo.isEmpty()){
    ja.add(jo);
}

For com.google.gson.JsonObject: 对于com.google.gson.JsonObject:

JsonObject jo = FetchData.getAllItemsAvg(
                          request.getParameter("where"),
                          request.getParameter("lastNum"),
                          request.getParameter("limitAvgNum"));
JsonArray ja = new JsonArray();
if(!jo.entrySet().isEmpty()){
    ja.add(jo);
}

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

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