从json数组中删除json对象

btxsgosb  于 2023-01-14  发布在  其他
关注(0)|答案(4)|浏览(256)

我有一个JsonArray,如下所示:

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

我想从上面的JsonArray中删除第二个JsonObject。为了做到这一点,我编写了以下代码:

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

但第二行抛出错误:java.lang.UnsupportedOperationException
有什么方法可以从JsonArray中删除JsonObject吗?

p4rjhz4m

p4rjhz4m1#

无法从JsonArray中删除元素,因为它不支持remove()方法:

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

remove()方法的实现来源于AbstractList

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

相反,为什么不创建一个单独的数据结构数组或列表来保存你想要的对象呢?
顺便说一下,使用JsonArray的目的是以对象形式加载json数据,这就是为什么它支持read方法,但不支持对加载的数据结构进行修改。

bttbmeg0

bttbmeg02#

可能是您的JsonElementJSONArray为空。
getJsonObject返回一个JSONObject。删除方法需要int
UnsupportedOperationException(如果不支持删除)。
试试这个:

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

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

并将结果分配给原始结果

fieldsObject=result;
fhity93d

fhity93d3#

  • gson

删除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);
    }

输出:

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

删除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);
        }

输出:

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}]
blpfk2vs

blpfk2vs4#

我尝试使用Sanj在上面的帖子中提到的org.json库。我们可以从JSONArray中删除元素,如下所示。我将json内容放在.txt文件中,并读入String对象以构造JSONObject。请参考下面的代码。

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);
}
}

相关问题