1

I have Dynamic JSON String i want to remove the last JSON object from the JSONARRAY in android. here is my dynamic JSON String in android. My json string is

("{\"findAllUsersResponse\": "+arguments[0].toString()+"}");

    {
    "findAllUsersResponse": [
        {
            "id": "kicJw2whXyuGjbNo936L",
            "name": "Fghhjj",
            "udid": "2AA120E3-7478-4AD4-9C68-9C0920669B84"
        },
        {
            "id": "NEF45TWNI6-Uc_r7938R",
            "name": "ssss",
            "udid": "1DD083C2-7F1D-4BB3-9AB9-691A5FD251CC"
        },
        {
            "id": "xuXY7Ah2-O-jL4Zk939D",
            "name": "Test",
            "udid": "A892E0AB-6732-4F42-BEFA-3157315E9EE4"
        },
        {
            "id": "w1FnBz8B9ciWUzBk939k",
            "name": "Aditi",
            "udid": "A892E0AB-6732-4F42-BEFA-3157315E9EE4"
        }

    ]
}
5
  • stackoverflow.com/questions/19837532/… Commented Sep 24, 2014 at 5:32
  • 1
    jsonArray.remove(jsonArray.length()-1); Commented Sep 24, 2014 at 5:33
  • But i have dynamic Json string. And i want to remove only the last JSON object Commented Sep 24, 2014 at 5:34
  • Sorry but i want to do it before API19... Commented Sep 24, 2014 at 5:41
  • @raja- i want to do it before API 19 Commented Sep 24, 2014 at 6:07

3 Answers 3

1

If your String is not going to change and you only want last object to be deleted use substring and create new string.

 String finduserjson=  "{\"findAllUsersResponse\": "+arguments[0].toString()+"}");
    String t = finduserjson.substring(finduserjson.indexOf("{"), finduserjson.lastIndexOf(",{"));
    String j = "]}";
    finduserjson = t+j;

Cheers

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

Comments

1

Was facing a similar problem. There are no methods in the JSONarray object to get what you want. You could make a function to remove it. Saw this solution somewhere.

   public static JSONArray remove(final int index, final JSONArray from) {
    final List<JSONObject> objs = getList(from);
    objs.remove(index);

    final JSONArray jarray = new JSONArray();
    for (final JSONObject obj : objs) {
        jarray.put(obj);
    }

    return jarray;
}

public static List<JSONObject> getList(final JSONArray jarray) {
    final int len = jarray.length();
    final ArrayList<JSONObject> result = new ArrayList<JSONObject>(len);
    for (int i = 0; i < len; i++) {
        final JSONObject obj = jarray.optJSONObject(i);
        if (obj != null) {
            result.add(obj);
        }
    }
    return result;
}

Cheers!

Comments

0

If you are using JSON rather than GSON or JACKSON, then you cannot call .remove() method because you would not find it there.

Best thing you can do is, re-create a new JSONArray and add required JSONObjects in it.

4 Comments

But bro response is coming from server and i want to remove the last json object everytime.
It doesn't matter. What JSON library are you using?
So you mean to say it is not possible.
What I am saying is that, with JSON.org library this cannot be done. If you are using some other library (which you actually should), then all you need to do is call something like foo.remove(foo.size() - 1);

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.