简体   繁体   中英

Remove json object from json array

I've a JsonArray like:

"fields" : [
    {
      "name":"First Name",
      "id":1
    },
    {
      "name":"Middle Name",
      "id":2
    },
    {
      "name":"Last Name",
      "id":3
    }
]

I want to remove second JsonObject from above JsonArray. In order to do that I' wrote following code:

JsonArray fieldsObject =jsonObject.getJsonArray("fields");
fieldsObject.remove(fieldsObject.getJsonObject(2));

But second line throws error: java.lang.UnsupportedOperationException

Is there any way, I can remove JsonObject from a JsonArray?

May be Your JsonElement or JSONArray is null.

getJsonObject returns a JSONObject . the remove method want int .

UnsupportedOperationException if removing is not supported.

Try This :

JSONArray fieldsObject =jsonObject.getJsonArray("fields");
fieldsObject.remove(int index);

OR

JSONArray result = new JSONArray();
for(int i=0;i<fieldsObject.length();i++)
{
   if(i!=2)
   {
    result.put(fieldsObject.get(i)); 
   }
}

and assign result to original one

fieldsObject=result;
  • gson library

Remove works for gson library version 2.3.1

public static void main(String[] args) throws Exception {


    String s = "{ \"fields\" : [ "+
            " {\"name\":\"First Name\",\"id\":1},"+
            "{\"name\":\"Middle Name\",\"id\":2},"+
            "{\"name\":\"Last Name\",\"id\":3}"+
            "]}";

        JsonParser parser = new JsonParser();
        JsonObject json = parser.parse(s).getAsJsonObject();
        System.out.println("original object:"+json);
        JsonArray fieldsObject = json.getAsJsonArray("fields");
        System.out.println("Before removal :"+fieldsObject);
        Object remove = fieldsObject.remove(1);
        System.out.println("After removal :"+fieldsObject);
    }

Output:

   original object:{"fields":[{"name":"First Name","id":1},{"name":"Middle Name","id":2},{"name":"Last Name","id":3}]}
Before removal :[{"name":"First Name","id":1},{"name":"Middle Name","id":2},{"name":"Last Name","id":3}]
After removal :[{"name":"First Name","id":1},{"name":"Last Name","id":3}]
  • org.json library

Remove works for org.json library

public static void main(String[] args) throws Exception {

            String s = "{ \"fields\" : [ "+
            " {\"name\":\"First Name\",\"id\":1},"+
            "{\"name\":\"Middle Name\",\"id\":2},"+
            "{\"name\":\"Last Name\",\"id\":3}"+
            "]}";
            JSONObject json = new JSONObject(s);
            System.out.println("original object:"+json);
            JSONArray fieldsObject =json.getJSONArray("fields");
            System.out.println("Before removal :"+fieldsObject);
            Object remove = fieldsObject.remove(1);
            System.out.println("After removal :"+fieldsObject);
        }

Output:

original object:{"fields":[{"name":"First Name","id":1},{"name":"Middle Name","id":2},{"name":"Last Name","id":3}]}
Before removal :[{"name":"First Name","id":1},{"name":"Middle Name","id":2},{"name":"Last Name","id":3}]
After removal :[{"name":"First Name","id":1},{"name":"Last Name","id":3}]

You can not remove element from JsonArray as it does not support remove() method:

private static final class JsonArrayImpl extends AbstractList<JsonValue> implements JsonArray {

And remove() method's implementation comes from AbstractList :

public E remove(int index) {
    throw new UnsupportedOperationException();
}

Instead why don't you create a separate data strucure array or list to hold the objects that you want?

By the way, the purpose of using JsonArray is to load json data in object form that is why it supports read methods but does not support modifications on the loaded data structure.

I tried using org.json library as mentioned by Sanj in the above post. We can remove the element from JSONArray as below. I placed the json content in the .txt file and read into the String object for constructing the JSONObject. Please refer the code below.

public class App{    
public static void main(String args[]) throws IOException{    

    BufferedReader br = new BufferedReader(new FileReader("json.txt"));
    String jsonContent = "";
    String jsonLine;
    while((jsonLine=br.readLine())!=null){
        jsonContent+=jsonLine;
    }
    JSONObject jObj = new JSONObject(jsonContent);
    JSONArray jsonArray = jObj.getJSONArray("fields");
    jsonArray.remove(1);
    System.out.println(jsonArray);
}
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM