user1410081
user1410081

Reputation:

Remove object from json response Android

Hello guys I need your help. How do I remove this from my json response?

[
  {
    "Woa": [
        "Seo",
        "Rikjeo",
        "JDa"
    ]
  },
"Aha",
"Aad",
"Char"
]

I want to remove this:

{
    "Woa": [
        "Seo",
        "Rikjeo",
        "JDa"
    ]
  }

This is what I tried so far:

for (int i = 0; i < array.length(); ++i) {
    list.add(array.getString(i));
}

list.remove(0);

But it is still not removed. How do I do that? Any ideas will be greatly appreciated

Edited list.remove(1) to (0)

Upvotes: 0

Views: 2147

Answers (6)

Dhaval Shingala
Dhaval Shingala

Reputation: 127

You Can one by one remove

 while (user_data.length() > 0) {
  user_data.remove(user_data.keys().next());
 }

Upvotes: 0

lhoak
lhoak

Reputation: 181

you can parse your string to JSONArray, the first element of the array is what you want.

@Test
public void test() {
    String str = "[  {\"Woa\": [\"Seo\",\"Rikjeo\",\"JDa\"]},\"Aha\",\"Aad\",\"Char\"]";
    try {
    JSONArray array;
        array = new JSONArray(str);
        System.out.println(array);
        System.out.println(array.get(0));
    } catch (JSONException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }

[{"Woa":["Seo","Rikjeo","JDa"]},"Aha","Aad","Char"]

{"Woa":["Seo","Rikjeo","JDa"]}

PASSED: test

Upvotes: 0

Garry
Garry

Reputation: 4533

After removing the item you need to create the JSON again using the list.

list.remove(1);

JSONArray jsArray = new JSONArray(list);

If you want to convert JSONArray to JSON string:

jsArray.toString()

Upvotes: 1

DDsix
DDsix

Reputation: 1984

You should make something that's more general so you don't have to modify your code EVERY time the JSON changes. For example:

        //here we create the JSONArray object from the string that contains the json data
        JSONArray array = new JSONArray(myJsonString);

        //here we create a list that represents the final result - a list of Strings
        List<String> list = new ArrayList<>();

        //here we parse every object that the JSONArray contains
        for (int i = 0; i < array.length(); i++) {

            //if the current item is an JSONObject, then we don't need it, so we continue our iteration 
            //this check is not necessary because of the below check, 
            //but it helps you see things clearly: we IGNORE JSONObjects from the array
            if (array.get(i) instanceof JSONObject) {
                continue;
            }

            //if our current object is a String, we add it to our final result
            if (array.get(i) instanceof String) {
                list.add(array.getString(i));
            }
        }

Upvotes: 0

Stephen C
Stephen C

Reputation: 718886

One problem is that the element you are trying to remove is the first one, and the index of the first element of a JSONArray object is zero.

You are calling list.remove(1) which removes the second element of the array.

The following should be sufficient:

list.remove(0);

... without the stuff prior to it.

If this is not working, then my guess is that you are removing the element too late; i.e. after the JSONArray object has been serialized. However, we need to see more (relevant) code to be sure.


The Android documentation for JSONArray fails to mention that array indexing is zero-based. However, it is. I checked the source code. Furthermore most other Java data structures (arrays, lists etcetera) use zero-based indexing. (A notable exception is Java data structures modelled on the W3C DOM APIs.)

Upvotes: 0

Uma Kanth
Uma Kanth

Reputation: 5629

Your list will be updated, not your JSON object.

Why don't you use a JSONparser.

JSONObject obj = new JSONObject(mystring);
obj.remove("entryname");

Upvotes: 0

Related Questions