2
JSONArray jsonArray = new JSONArray();
jsonArray.put(1);
jsonArray.put("empty");
jsonArray.put(2);
jsonArray.put(3);
jsonArray.put("empty");
jsonArray.put(4);
jsonArray.put("empty");

lets say we have this jsonArray, there are strings empty, how to remove them without leaving gaps?

asked Dec 19, 2013 at 12:27
4
  • JSONArray from which API you are using ? Commented Dec 19, 2013 at 12:34
  • iam using this import org.json.JSONArray; Commented Dec 19, 2013 at 12:36
  • already we have link with solution stackoverflow.com/questions/18497162/… Commented Dec 19, 2013 at 12:38
  • @KarthikeyanSukkoor the link you pointed out is for javascript. The question is on Java and was tagged in java. There is no splice() in JsonArray in java. Commented Dec 19, 2013 at 13:11

5 Answers 5

2

You can use the following code :

for (int i = 0, len = jsonArray.length(); i < len; i++) {
 JSONObject obj = jsonArray.getJSONObject(i);
 String val = jsonArray.getJSONObject(i).getString();
 if (val.equals("empty")) { 
 jsonArray.remove(i);
 }
}
Gastón Saillén
13.3k5 gold badges76 silver badges82 bronze badges
answered Dec 19, 2013 at 12:38
Sign up to request clarification or add additional context in comments.

Comments

0

take a look at this post. Make a list to put the indexes of the elements with the "empty" string and iterate over your list (the one with the "empty" elements) saving the indexes of the items to delete. After that, iterate over the previosly saved indexes and use

list.remove(position);

where position takes the value of every item to delente (within the list of index).

answered Dec 19, 2013 at 12:48

Comments

0

Should work with few fixes to Keerthi's code:

for (int i = 0; i < jsonArray.length(); i++) {
 if (jsonArray.get(i).equals("empty")) {
 jsonArray.remove(i);
 }
}
Mr. Polywhirl
49.2k12 gold badges96 silver badges147 bronze badges
answered Dec 19, 2013 at 12:48

1 Comment

Note that versions with getString() will not work because first element is not a String...
0

You can use this following code

JSONArray list = new JSONArray(); 
JSONArray jsonArray = new JSONArray(jsonstring); 
int len = jsonArray.length();
if (jsonArray != null) { 
 for (int i=0;i<len;i++)
 { 
 //Excluding the item string equal to "empty"
 if (!"empty".equals(jsonArray.getString(i)) 
 {
 list.put(jsonArray.get(i));
 }
 } 
 //Now, list JSONArray has no empty string values
}
answered Dec 19, 2013 at 12:40

Comments

0

you can use string replace after converting the array to string as,

 JSONArray jsonArray = new JSONArray();
 jsonArray.put(1);
 jsonArray.put("empty");
 jsonArray.put(2);
 jsonArray.put(3);
 jsonArray.put("empty");
 jsonArray.put(4);
 jsonArray.put("empty");
 System.err.println(jsonArray);
 String jsonString = jsonArray.toString();
 String replacedString = jsonString.replaceAll("\"empty\",", "").replaceAll("\"empty\"", "");
 jsonArray = new JSONArray(replacedString);
 System.out.println(jsonArray);

Before replace:

jsonArray is [1,"empty",2,3,"empty",4,"empty"]

After replace:

jsonArray is [1,2,3,4]
answered Dec 19, 2013 at 13:08

Comments

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

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.