java 移除 JSON 对象

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/33605452/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-11-02 21:54:32  来源:igfitidea点击:

Remove object of JSON

javajsonHymanson

提问by Gavin

How do I remove the object of JSON? I am using Hymanson API 2.6.3

如何删除JSON的对象?我正在使用Hyman逊 API 2.6.3

Example of my JSON String

我的 JSON 字符串示例

{  
   "movieList":[  
      {  
         "movieID":1,
         "title":"TITLE 1",
         "type":"DIGITAL",
         "status":"COMING SOON",
         "synopsis":null,
         "director":null,
         "mRating":"G",
         "casts":null,
         "showTimes":[  
            {  
               "date":"01/12/15",
               "time":"22:00"
            },
            {  
               "date":"01/12/15",
               "time":"23:30"
            }
         ]
      }
   ]
}

I would like to be able to remove the whole showTimes object given its index.

我希望能够删除整个 showTimes 对象的索引。

Something like showtimesList.get(index).remove()And if it is the last object in the arrayList, the value should be set to null.

喜欢的东西showtimesList.get(index).remove()而如果是在ArrayList中的最后一个对象,该值应设置为null

As suggested by one of the answer, I am converting the JAVA Object ShowTimeto JSONNode by so

正如其中一个答案所建议的那样,我正在将 JAVA 对象转换ShowTime为 JSONNode

ObjectMapper objectMapper = new ObjectMapper();
JsonNode showTimesNode = objectMapper.convertValue(movieList.get(index).getShowTimes(), JsonNode.class);
Iterator<JsonNode> itr = showTimesNode.iterator();
int counter = 1;
while(itr.hasNext() && counter<=showTimeChoice){
    if(counter==showTimeChoice){
        itr.remove();
        Cineplex.updateDatabase(cineplexList);
        System.out.println("Sucessfully removed!");
        break;
    }
    counter++;
}

But it's throwing the error Exception in thread "main" java.lang.IllegalStateException at java.util.ArrayList$Itr.remove(Unknown Source)when I tries to remove the 2nd element of showTimeson the above given JSON String

但是Exception in thread "main" java.lang.IllegalStateException at java.util.ArrayList$Itr.remove(Unknown Source)当我尝试删除showTimes上面给定的 JSON 字符串中的第二个元素时,它会抛出错误

Which is

这是

{  
  "date":"01/12/15",
  "time":"23:30"
}

回答by Amadan

Something like this should work (I'm not a Hymanson user, so YMMV):

这样的事情应该可行(我不是 Hymanson 用户,所以 YMMV):

((ObjectNode) movieListElement).remove("showTimes");

EDIT:

编辑:

JsonNode movieListElement = ((ArrayNode) root.path("movieList").get(index);

回答by sunysen

for (JsonNode personNode : rootNode) {
    if (personNode instanceof ObjectNode) {
       if (personNode.has("showTimes")) {
          ObjectNode object = (ObjectNode) personNode;  
          object.remove("showTimes");
       }
    }
}

回答by Sudheer

Something like this should work

这样的事情应该工作

public void removeShowTime(int pos){
    final JsonNode movieList = new ObjectMapper().readTree(json).get("movieList").get(0);
    final JsonNode showList = movieList.get("showtimesList");
    Iterator<JsonNode> itr = showList.iterator();
    int counter = 0
    while(itr.hasNext() && counter<=pos){
       if(counter==pos){
           itr.remove();
       }
       counter++;
    }
}