简体   繁体   中英

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

I want to convert my Blank Json array to the Null Json array.

For ex., My Json array is like "[{}]" and if I got this array then automatically converts to "[]".

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 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:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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