Search for specific value in a deep nested JsonArray/JsonObject - java

I have a deep nested JsonObject like this, what is the best way to search for a specific value (null in this example) in this object?
{
"monitors" : ["monitor1"],
"index" : [{
"patterns" : [ "*" ],
"masked" : [ "abcd", "*ip_dest*::/[0-9]{1,3}$/::XXX"],
"allowed" : [ "123", null ]
}],
"permissions" : [ ]
}
For this example, I have a list of keys, I want to get the values for those keys, check if the value has Array type and if yes, search if there is any null in that array. Here is the code I have:
for (Entry<String, DataType> allowedKey : allowedKeys.entrySet()) {
DataType dataType = allowedKey.getValue();
JsonNode value = contentAsNode.get(allowedKey.getKey());
if (dataType == DataType.ARRAY && value != null) {
try {
List contentArray = DefaultObjectMapper.objectMapper.convertValue(value, java.util.List.class);
if (contentArray.contains(null)) {
this.errorType = ErrorType.NULL_ARRAY_ELEMENT;
return false;
}
} catch (Exception e) {
this.errorType = ErrorType.BODY_NOT_PARSEABLE;
return false;
}
}
}
However contains() can not find null in this case, because I have a nested array. Since the structure of the Json object could be different each time (it could have nested array or maps or just an array), I was wondering what is the best way to parse a deep nested JsonObject to find a specific value?
More clarification: in the above example Json, the key that I am interested in is index, the value of this key, is a map (but it could be an array or nested array as well, we do not know beforehand), I want to check if there is any null in index values(which in this case, there is a null)

One viable easy solution using JSONPath
public static void main(String[] args) {
String json = "{\r\n" +
" \"monitors\" : [\"monitor1\"],\r\n" +
" \"index\" : [{\r\n" +
" \"patterns\" : [ \"*\" ],\r\n" +
" \"masked\" : [ \"abcd\", \"*ip_dest*::/[0-9]{1,3}$/::XXX\"],\r\n" +
" \"allowed\" : [ \"123\", null ]\r\n" +
" }],\r\n" +
" \"permissions\" : [ ]\r\n" +
"}" ;
String path = "$.index[?(null in #.allowed)]"; //Check if allowed List has null value i.e. index->allowed
DocumentContext jsonContext = JsonPath.parse(json);
List<?> list = jsonContext.read(path);
if(list.isEmpty()) { //Based on empty List u can return true or false
System.out.println("Not found");
}else {
System.out.println("Found");
}
}
As per OP requirement psoting another solution iterate over JsonObject recursively
public static void main(String[] args) {
String json = "{\r\n" +
" \"monitors\" : [\"monitor1\"],\r\n" +
" \"index\" : [{\r\n" +
" \"patterns\" : [ \"*\" ],\r\n" +
" \"masked\" : [ \"abcd\", \"*ip_dest*::/[0-9]{1,3}$/::XXX\"],\r\n" +
" \"allowed\" : [ \"123\", null ],\r\n" +
" \"country\" : \"India\"\r\n" +
" }],\r\n" +
" \"permissions\" : [ ]\r\n" +
"}" ;
try {
iterateJson(new JSONObject(json));
} catch (Exception e) {
e.printStackTrace();
}
}
public static void iterateJson(JSONObject jsonObject) throws Exception {
ObjectMapper mapper = new ObjectMapper();
Iterator<String> iterator = jsonObject.keys();
while(iterator.hasNext()) {
String key = iterator.next();
if(jsonObject.get(key) instanceof JSONArray) {
JSONArray jsonArray = jsonObject.getJSONArray(key);
for(int i=0; i<jsonArray.length(); i++) {
if(jsonArray.get(i) instanceof JSONObject) {
iterateJson(jsonArray.getJSONObject(i));
}else if(jsonArray.get(i) instanceof String){
List<String> list = mapper.readValue(jsonArray.toString(), new TypeReference<List<String>>() {});
System.out.println(key+" :: "+list);
System.out.println("Contains null :: "+list.contains(null));
System.out.println();
break;
}
}
}else if(jsonObject.get(key) instanceof JSONObject) {
iterateJson(jsonObject.getJSONObject(key));
}
}
}
output
masked :: [abcd, *ip_dest*::/[0-9]{1,3}$/::XXX]
Contains null :: false
allowed :: [123, null]
Contains null :: true
patterns :: [*]
Contains null :: false
monitors :: [monitor1]
Contains null :: false

Related

Object automatically being set to null in reflection java

I have written a reflection function which merges two objects on some pre-specified conditions :
While iterating on the fields of the object,the value of object A is correct as observed but as soon as the fields of the current object are exhausted and the function starts iterating on the next object, the fields to which values had been set ,they become NULL.
Here, I provide my reflection code and the main function :
public static void mergeObjectAndSet(Object objectA,Object objectB,Set<String> deleteSetA,Set<String> deleteSetB,Set<String> deleteSetC,String path) throws Exception {
LOGGER.info("In merge Sets and Diff function : ");
LOGGER.info("Deleted sets A are : "+deleteSetA+" B : "+deleteSetB+" C : "+deleteSetC);
LOGGER.info("Object A is : "+objectA+" object B is : "+objectB);
if(null == deleteSetA){
deleteSetA = new HashSet<>();
}
else if(null == deleteSetB){
deleteSetB = new HashSet<>();
}
Class classA = objectA.getClass();
Class classB = objectB.getClass();
LOGGER.info("Classes are : "+classA+" class B : "+classB);
Field objectBFields[] = classB.getDeclaredFields();
System.out.println(objectA);
for (Field fieldB : objectBFields) {
LOGGER.info("fields to be looped in mergeObjectAndSet are : "+objectBFields.toString() +" path is : "+path);
fieldB.setAccessible(true);
Class typeB = fieldB.getType();
Object fieldAObject = fieldB.get(objectA);
Object fieldBObject = fieldB.get(objectB);
if (!Collection.class.isAssignableFrom(typeB)) {
LOGGER.info("passes null check for field objects : ");
if (isTypePrimitive(typeB)) {
if(null != fieldAObject || null != fieldBObject) {
Field fieldA = classA.getDeclaredField(fieldB.getName());
fieldA.setAccessible(true);
LOGGER.info("field A is : " + fieldA.getName());
if (!(deleteSetA.contains(path + Constants.HYPHEN + fieldA.getName())) && (deleteSetB.contains(path + Constants.HYPHEN + fieldA.getName()))) {
LOGGER.info("In only deleted set case : Adding field : " + fieldA.getName() + " to deleted set");
deleteSetC.add(path + Constants.HYPHEN + fieldA.getName());
LOGGER.info("Merged object for the field : " + fieldB.getName() + " to object : " + fieldBObject + " in object : " + objectA);
}else if(deleteSetA.contains(path + Constants.HYPHEN + fieldA.getName()) && !(deleteSetB.contains(path + Constants.HYPHEN + fieldA.getName())) && null != fieldBObject && null==fieldAObject) {
LOGGER.info("in merge set and objects case : ");
fieldA.set(objectA, fieldBObject);
LOGGER.info("Merged object for the field for case refresh : " + fieldB.getName() + " to object : " + fieldBObject + " in object : " + objectA);
}else if(!(deleteSetA.contains(path + Constants.HYPHEN + fieldA.getName())) && !(deleteSetB.contains(path + Constants.HYPHEN + fieldA.getName()))){
LOGGER.info("In merge case irrespective of deleted sets : ");
fieldA.set(objectA, fieldBObject);
}
}
} else {
if ((null==fieldAObject && null==fieldBObject)){
LOGGER.info("In both fields are null case : ");
LOGGER.info("Field here is : "+fieldB.getName());
for(String del : deleteSetA){
if(del.startsWith(fieldB.getName())){
deleteSetC.addAll(deleteSetA);
}
}
continue;
}
LOGGER.info("In non primitive type check : path here for np is : "+path);
LOGGER.info("field name here : "+fieldB.getName());
LOGGER.info("path here : "+path);
LOGGER.info("object name here : "+objectA.getClass().getName());
Field fieldA = classA.getDeclaredField(fieldB.getName());
fieldA.setAccessible(true);
if (null == fieldAObject) {
LOGGER.info("if A is null case : initialise it with an instance");
Constructor[] constructors = fieldA.getType().getDeclaredConstructors();
for (Constructor constructor : constructors) {
constructor.setAccessible(true);
if (0 == constructor.getParameterCount()) {
fieldAObject = constructor.newInstance();
break;
}
}
}
LOGGER.info("No test cases met, path is : "+path);
if (null == path) {
LOGGER.info("when path is null new path here is : "+path);
mergeObjectAndSet(fieldAObject, fieldBObject, deleteSetA, deleteSetB, deleteSetC, fieldA.getName());
} else {
LOGGER.info("path here when some path is there is : "+path);
mergeObjectAndSet(fieldAObject, fieldBObject, deleteSetA, deleteSetB, deleteSetC, path + Constants.HYPHEN + fieldA.getName());
}
}
}
}
}
The main function is :
public static void main(String args[]) {
LeadDetailSRO leadDetailSRO1 = new LeadDetailSRO();
LeadDetailSRO leadDetailSRO2 = new LeadDetailSRO();
BankDetailSRO bankDetails = new BankDetailSRO();
bankDetails.setBeneficiaryName("ars");
bankDetails.setBranchName("noida");
leadDetailSRO2.setBankDetails(bankDetails);
Set<String> deleteSet1 = new HashSet<>();
Set<String> deleteSet2 = new HashSet<>();
Set<String> deleteSet3 = new HashSet<>();
deleteSet1.add("bankDetails-beneficiaryName");
try {
System.out.println("Before deletion object 1 is : " + leadDetailSRO1 + " object 2 is : " + leadDetailSRO2+"deleteset A is : "+deleteSet1+" B is : "+deleteSet2+" C is : "+deleteSet3);
Utils.mergeObjectAndSet(leadDetailSRO1, leadDetailSRO2, deleteSet1, deleteSet2, deleteSet3, null);
System.out.println("After deletion object 1 is : " + leadDetailSRO1 + " object 2 is : " + leadDetailSRO2+"deleteset A is : "+deleteSet1+" B is : "+deleteSet2+" C is : "+deleteSet3);
} catch (Exception e) {
e.printStackTrace();
}
}
The output is :
After deletion object 1 is: LeadDetailSRO{
uploadDocumentList=null,
businessOwnerDetails=null,
businessOwnerDetailList=null,
authorizedSignatoryList=null,
businessEntityDetails=null,
leadInfo=null,
bankDetails=null,
addressDetails=null,
cfaAgent=null,
vaAgent=null,
auditTrail=null,
additionalDetails=null,
additionalQuestions=null,
}
object2 is: LeadDetailSRO{
uploadDocumentList=null,
businessOwnerDetails=null,
businessOwnerDetailList=null,
authorizedSignatoryList=null,
businessEntityDetails=null,
leadInfo=null,
bankDetails=BankDetailSRO{
bankName='null',
bankAccountNumber='null',
ifscCode='null',
bankAccountHolder='null',
beneficiaryName='ars',
branchName='noida',
status='null',
nameMatchStatus='null',
reEnterAccountNumber='null',
reEnterIfscCode='null'
},
addressDetails=null,
cfaAgent=null,
vaAgent=null,
auditTrail=null,
additionalDetails=null,
additionalQuestions=null,
documents=null,
}
deleteset A is: [
bankDetails-beneficiaryName
]Bis: [
]Cis: [
]
Whereas the expected output is :
After deletion object1 is: LeadDetailSRO{
uploadDocumentList=null,
businessOwnerDetails=null,
businessOwnerDetailList=null,
authorizedSignatoryList=null,
businessEntityDetails=null,
leadInfo=null,
bankDetails=BankDetailSRO{
bankName='null',
bankAccountNumber='null',
ifscCode='null',
bankAccountHolder='null',
beneficiaryName='ars',
branchName='noida',
status='null',
nameMatchStatus='null',
reEnterAccountNumber='null',
reEnterIfscCode='null'
},
addressDetails=null,
cfaAgent=null,
vaAgent=null,
auditTrail=null,
additionalDetails=null,
additionalQuestions=null,
documents=null,
}
object2 is: LeadDetailSRO{
uploadDocumentList=null,
businessOwnerDetails=null,
businessOwnerDetailList=null,
authorizedSignatoryList=null,
businessEntityDetails=null,
leadInfo=null,
bankDetails=BankDetailSRO{
bankName='null',
bankAccountNumber='null',
ifscCode='null',
bankAccountHolder='null',
beneficiaryName='ars',
branchName='noida',
status='null',
nameMatchStatus='null',
reEnterAccountNumber='null',
reEnterIfscCode='null'
},
addressDetails=null,
cfaAgent=null,
vaAgent=null,
auditTrail=null,
additionalDetails=null,
additionalQuestions=null,
documents=null,
}
deleteset A is: [
bankDetails-beneficiaryName
]Bis: [
]Cis: [
]
The difference in outputs is to be seen in object1 bankDetails
LeadDetailSRO is a class which contains BankDetailSRO within it as an object and BankDetailSRO contains fields beneficiaryName,branchName.
When fieldA is null, you create a new object, but you never set the field with this new instance.
Reference fieldAObject does not point to the null value from fieldA anymore but on the new instance you created. But objectA does not reference this object.
You have to explicitly write it.
if (null == fieldAObject) {
LOGGER.info("if A is null case : initialise it with an instance");
Constructor[] constructors = fieldA.getType().getDeclaredConstructors();
for (Constructor constructor : constructors) {
constructor.setAccessible(true);
if (0 == constructor.getParameterCount()) {
fieldAObject = constructor.newInstance();
fieldA.set(objectA, fieldAObject); // <-- set the new value
break;
}
}

Java JSON: get object-value without quotes?

I parse a JSON string.
How can I get the value of an object without the quotes? I get it with lists but it is not working with objects.
import javax.json.*;
import javax.json.stream.*;
...
String s="{ \"foo\" : [ \"abc\", \"def\" ], \"bar\" : { \"k1\" : \"v1\", \"k2\" : \"v2\" } }";
JsonReader jr = Json.createReader(new StringReader (s));
JsonObject jo = jr.readObject();
JsonArray foo = jo.getJsonArray ("foo");
for (int i = 0; i < foo.size (); i ++)
{
System.out.println (i + ": " + foo.getString (i)); // ok, no quotes
}
JsonObject bar = jo.getJsonObject ("bar");
for (Map.Entry <String, JsonValue> e : bar.entrySet ())
{
System.out.println (e.getKey() + ": " + e.getValue ().toString());
}
Thats the output.
0: abc
1: def
k1: "v1"
k2: "v2"
How can I get v1 and v2 without quotes out of the parser?
You know your input value are String, so you can do this:
for (Map.Entry <String, JsonValue> e : bar.entrySet ()) {
System.out.println (e.getKey() + ": " + ((JsonString)e.getValue ()).getString());
}

How can I structurally modify a JsonObject and replace some of its values?

I have a json in a JsonObject such as:
"customer": {
"full_name": "John John",
"personal": {
"is_active": "1",
"identifier_info": {
"hobbies": [
"skiing",
"traveling"
],
"user_id": "1234",
"office_id": "2345"
}
}
}
Is there a way to modify the JsonObject so as to remove the hobbies completely from the identifier_info and leave the rest untouched and then add the contents of hobbies the same way as the others? i.e.
"customer": {
"full_name": "John John",
"personal": {
"is_active": "1",
"identifier_info": {
"skiing":"expert",
"traveling":"rarely",
"user_id": "1234",
"office_id": "2345"
}
}
}
Find the full implementation for removing the JSON Array "hobbies" and to insert them in the parent JSON Object directly.
public class ProcessJSONString {
String data ="{\"customer\": { \n" +
" \"full_name\": \"John John\", \n" +
" \"personal\": { \n" +
" \"is_active\": \"1\", \n" +
" \"identifier_info\": { \n" +
" \"hobbies\": [ \n" +
" \"skiing\",\n" +
" \"traveling\"\n" +
" ], \n" +
" \"user_id\": \"1234\", \n" +
" \"office_id\": \"2345\"\n" +
" } \n" +
" } \n" +
"}} ";
public void processData() {
try {
JSONObject result = new JSONObject(data);
JSONObject customer = result.getJSONObject("customer");
JSONObject personal = customer.getJSONObject("personal");
JSONObject identifierInfo =
personal.getJSONObject("identifier_info");
JSONArray hobbies = identifierInfo.getJSONArray("hobbies");
identifierInfo.remove("hobbies");
//Under the assumption the tags will be added by the user, the code has been inserted.
identifierInfo.put("skiing","expert");
identifierInfo.put("traveling","rarely");
} catch (JSONException e) {
e.printStackTrace();
}
}
public static void main(String[] args) {
ProcessJSONString instance = new ProcessJSONString();
instance.processData();
}
}
That should be no problem, using JsonObject's remove() method. It returns the removed JsonElement. Assuming the original json is a JsonObject called customer:
JsonObject identifierInfo = customer.getAsJsonObject("personal").getAsJsonObject("identifier_info");
JsonArray hobbies = (JsonArray) identifierInfo.remove("hobbies");
after that you can just add the hobbies to the identifierInfo and get the desired result:
for (JsonElement aHobby : hobbies) {
identifierInfo.addProperty(aHobby.getAsString(), "expert");
}
don't forget to add null checks as needed.

Removing the bracket and empty string

The end result of the code is to be able to parse the string array to integer array but as you can see I get an exception. I tried to split the [ and ] signs but when I call the first (0) element splited_game_result[0] of the array, it's returns nothing. What is the solution here? I tried using trim method.
String[] ca_po_pe = {"4:7","PAUSE","2:0","1:3 PINE","PAUSE","CANCEL","2:4","0:5 PINE","PAUSE","CANCEL"};
String canc_postp_pen_games = "";
boolean confirming = true;
for(String looped_games : ca_po_pe) {
if(confirming) { canc_postp_pen_games+=looped_games; confirming=false; }
else { canc_postp_pen_games+=", "+looped_games; } }
System.out.println("LOOPED FINAL RESULT GAMES TO INSERT COMMAS: " + "\n" + canc_postp_pen_games + "\n");
String[] fin_games_res = canc_postp_pen_games.split("[,]");
ArrayList<String> arraylist= new ArrayList<String>();
Collections.addAll(arraylist, fin_games_res);
for (String str: arraylist) {
}
System.out.println("ELEMENTS ADDED TO ARRAYLIST: " + arraylist + "\n");
int noItems = arraylist.size();
for (int i = 0; i < noItems; i++) {
String currItem = arraylist.get(i);
if (currItem.contains("PAUSE")) {
arraylist.add(new String("400"));
noItems++; }
if (currItem.contains("CANCEL")) {
arraylist.add(new String("300"));
noItems++; }
if (currItem.contains(" PINE")) {
arraylist.add(new String("500"));
noItems++; }
}
System.out.println("ELEMENTS BEFORE REMOVAL: " + "\n" + arraylist + "\n");
Iterator<String> iter_getting = arraylist.iterator();
while(iter_getting.hasNext()) {
if(iter_getting.next().contains("PAUSE")){
iter_getting.remove(); }}
Iterator<String> iter_getting1 = arraylist.iterator();
while(iter_getting1.hasNext()) {
if(iter_getting1.next().contains("CANCEL")){
iter_getting1.remove(); }}
Iterator<String> iter_getting2 = arraylist.iterator();
while(iter_getting2.hasNext()) {
if(iter_getting2.next().contains(" PINE")){
iter_getting2.remove(); }}
System.out.println("ELEMENTS AFTER REMOVAL: " + "\n" + arraylist);
System.out.println("ELEMENT ON INDEX 3 BEFORE CONVERTION TO ARRAY: " + "\n" + arraylist.get(3));
String convert = arraylist.toString();
System.out.println("CONVERTED STRING: " + convert);
String[]splited_game_result = convert.trim().split("[\\[:,\\]]");
System.out.println(" AFTER SPLITING TO ARRAY: " + "\n" + splited_game_result[0]);
Integer[] final_result = new Integer[splited_game_result.length];
int g = 0;
for(String fsgr : splited_game_result)
{ final_result[g] = Integer.parseInt(fsgr); g++;}
System.out.println("INTEGER ELEMENT ON INDEX 0: " + "\n" + final_result[1]);
For everybody who wonders, exception is raised in the very end of the source. It says NumberFormatException: For input string: "". See source below:
for(String fsgr : splited_game_result)
{ final_result[g] = Integer.parseInt(fsgr); g++;}
The reason is in the regular expession author used to split the array:
String[]splited_game_result = convert.trim().split("[\\[:,\\]]");
It produces the following array of strings:
["", "4", "7", " 2", "0", " 2", "4", " 400", " 500", " 400", " 300", " 500", " 400", " 300"]
Issue is that Integer.parseInt requires string that contain parsable integer
Possible workaround is to add if condition to ensure your fsgr is not an empty string and doesn't contain whitespaces:
for(String fsgr : splited_game_result) {
String fsgrTrim = fsgr.trim();
if (!fsgrTrim.isEmpty()) {
final_result[g] = Integer.parseInt(fsgrTrim);
g++;
}
}
Or to add try-catch clause:
for(String fsgr : splited_game_result) {
try {
final_result[g] = Integer.parseInt(fsgr);
g++;
} catch (Exception e) {
e.printStackTrace();
}
}
I still don't know what you're trying to do, but here is your exception. You are parsing an empty string and trying to parse a space before the number. Put comments in for your logic.
for (String fsgr : splited_game_result) {
try {
final_result[g] = Integer.parseInt(fsgr);
g++;
} catch(Exception e2) {
System.out.println("Exception = ["+ fsgr + "]");
}
}
Output (in middle of put. Use first and last printlns as location)
CONVERTED STRING: [4:7, 2:0, 2:4, 400, 500, 400, 300, 500, 400, 300]
AFTER SPLITING TO ARRAY:
Exception = []
Exception = [ 2]
Exception = [ 2]
Exception = [ 400]
Exception = [ 500]
Exception = [ 400]
Exception = [ 300]
Exception = [ 500]
Exception = [ 400]
Exception = [ 300]
INTEGER ELEMENT ON INDEX 0:
7

How to delete specific json object in jsonarray in java

I have json array from server response like this.
[
{
"idApp" : "001"
"AppName" : "Test App 1"
},
{
"idApp" : "002"
"AppName" : "Test App 2"
},
{
"idApp" : "003"
"AppName" : "Test App 3"
},
{
"idApp" : "004"
"AppName" : "Test App 4"
}
]
i just want to know the position of this object in jsonarray programatically
{
"idApp" : "003"
"AppName" : "Test App 3"
}
This should work for you
for(int i = 0 ; i < arguments.length(); i++){
if(arguments.getObject(i).get("idApp").asString().equals("003"))
System.out.println("Found it : " + i);
}
You forget , in each object in json string after each data like below :
{
"idApp" : "003",
"AppName" : "Test App 3"
}
By the way to get postion match 003 in idApp we can use Gson library http://mvnrepository.com/artifact/com.google.code.gson/gson/2.3.1
Add dependency in pom.xml :
<dependency>
<groupId>com.google.code.gson</groupId>
<artifactId>gson</artifactId>
<version>2.3.1</version>
</dependency>
Create modal class like your json object :
public class Modal {
private String idApp;
private String AppName;
public String getIdApp() {
return idApp;
}
public void setIdApp(String idApp) {
this.idApp = idApp;
}
public String getAppName() {
return AppName;
}
public void setAppName(String AppName) {
this.AppName = AppName;
}
}
Now convert json string to array of object and loop over array and find match object like below :
public class JsonUtils {
public static void main(String[] args) {
System.out.println("Position for 003 is = " + new JsonUtils().getPositionFromJsonString());
}
public int getPositionFromJsonString() {
Gson gson = new Gson();
String jsonString = "["
+ " {"
+ " \"idApp\" : \"001\","
+ " \"AppName\" : \"Test App 1\""
+ " },"
+ ""
+ " {"
+ " \"idApp\" : \"002\","
+ " \"AppName\" : \"Test App 2\""
+ " },"
+ ""
+ " {"
+ " \"idApp\" : \"003\","
+ " \"AppName\" : \"Test App 3\""
+ " },"
+ ""
+ " {"
+ " \"idApp\" : \"004\","
+ " \"AppName\" : \"Test App 4\""
+ " }"
+ ""
+ "]";
Modal[] modals = gson.fromJson(jsonString, Modal[].class);
int pos = -1;
for (Modal m : modals) {
pos++;
if ("003".equalsIgnoreCase(m.getIdApp())) {
return pos;
}
}
return -1;
}
}
You can do something like that to remove the specefic jsonObject from jsonArray.
//find the value in your jsonarray
for (int i = 0; i < jsonarray.length; ++i)
{
JSONObject rec =jsonarray.getJSONObject(i);
//check the condition if the given id is associated with the object then remove it
if(rec.getString("idApp").equals("your_passedId")
{
jsonarray.remove(i)
}
}
May be late, but it may help to someone
int index = 0;
while (index < yourArray.length()) {
JSONObject object = yourArray.optJSONObject(index);
if (object != null) {
if (/*your condition is true*/ true){
yourArray.remove(index);
index--;
}
}
index++;
}

Categories

Resources