1
JSONObject sss = arr_cat_details_old.getJSONObject(0).getJSONArray("pac_selected").getJSONObject(1);
         sss.remove("pack_selected_id");

"pac_selected": [{
        "pack_selected_id": "7"
    }, {}, {
        "pack_selected_id": 45
    }]
1
  • Consider adding more details to your question Commented Jan 7, 2017 at 10:10

2 Answers 2

1

assuming:

String jsonString = "pac_selected": [{
    "pack_selected_id": "7"
}, {}, {
    "pack_selected_id": 45
}]

I would do the following, based on this answer:

JSONArray list = new JSONArray();     
JSONArray jsonArray = new JSONArray(jsonstring); 
int len = jsonArray.length();

if (jsonArray != null) { 
   for (int i=0;i<len;i++)
   { 
       String element = jsonArray.get(i);

       if (!element.isEmpty()) 
       {
          list.put(element);
       }
   } 
}

Then just use list instead of jsonArray.

Sign up to request clarification or add additional context in comments.

Comments

0

Try this method

   String json = .... // your json

   Type type = new TypeToken<Map<String, Object>>() {}.getType();
   Map<String, Object> data = new Gson().fromJson(json, type);

   for (Iterator<Map.Entry<String, Object>> it = data.entrySet().iterator(); it.hasNext();) {
            Map.Entry<String, Object> entry = it.next();
            if (entry.getValue() == null) {
                it.remove();
            } else if (entry.getValue().getClass().equals(ArrayList.class)) {
                if (((ArrayList<?>) entry.getValue()).size() == 0) {
                    it.remove();
                }
            }
        }

    String json1 = new GsonBuilder().setPrettyPrinting().create().toJson(data);
    System.out.println("Latest json "+json1);

Add this to pom.xml

<dependency>
    <groupId>com.google.code.gson</groupId>
    <artifactId>gson</artifactId>
    <version>2.8.2</version>
</dependency>

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.