简体   繁体   English

从 json 数组中删除 json 对象

[英]Remove json object from json array

I've a JsonArray like:我有一个 JsonArray 像:

"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.我想从上面的 JsonArray 中删除第二个 JsonObject。 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但第二行抛出错误: java.lang.UnsupportedOperationException

Is there any way, I can remove JsonObject from a JsonArray?有什么办法,我可以从 JsonArray 中删除 JsonObject 吗?

May be Your JsonElement or JSONArray is null.可能是您的JsonElementJSONArray为空。

getJsonObject returns a JSONObject . getJsonObject返回一个JSONObject the remove method want int . remove 方法需要int

UnsupportedOperationException if removing is not supported.如果不支持移除,则出现UnsupportedOperationException

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 gson图书馆

Remove works for gson library version 2.3.1删除gson库版本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 org.json

Remove works for org.json library删除org.json库的作品

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:您不能从JsonArray中删除元素,因为它不支持remove()方法:

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

And remove() method's implementation comes from AbstractList :remove()方法的实现来自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.顺便说一句,使用JsonArray的目的是以对象形式加载 json 数据,这就是它支持读取方法但不支持对加载的数据结构进行修改的原因。

I tried using org.json library as mentioned by Sanj in the above post.我尝试使用 Sanj 在上面的帖子中提到的 org.json 库。 We can remove the element from JSONArray as below.我们可以从 JSONArray 中删除元素,如下所示。 I placed the json content in the .txt file and read into the String object for constructing the JSONObject.我将 json 内容放在 .txt 文件中,并读入 String 对象以构建 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);
}
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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