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
}]
Asked
Active
Viewed 4,480 times
1
-
Consider adding more details to your question – Gurwinder Singh Jan 07 '17 at 10:10
2 Answers
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.

Community
- 1
- 1

IntelliData
- 441
- 6
- 29
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>

John Joe
- 12,412
- 16
- 70
- 135