从json数组中删除json对象

时间:2016-03-31 06:12:39

标签: java json

我是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吗?

4 个答案:

答案 0 :(得分:0)

可能是您的JsonElementJSONArray为空。

getJsonObject返回JSONObject。  remove方法需要int

UnsupportedOperationException 如果不支持删除。

试试这个:

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

并将结果分配给原始

fieldsObject=result;

答案 1 :(得分:0)

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

答案 2 :(得分:0)

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

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

remove()方法的实施来自AbstractList

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

相反,为什么不创建单独的数据结构数组或列表来保存您想要的对象?

顺便说一句,使用JsonArray的目的是以对象形式加载json数据,这就是它支持读取方法但不支持对加载的数据结构进行修改的原因。

答案 3 :(得分:0)

我尝试使用上面帖子中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);
}
}