删除JSON的对象

时间:2015-11-09 08:53:53

标签: java json jackson

如何删除JSON的对象?我使用的是Jackson API 2.6.3

我的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"
            }
         ]
      }
   ]
}

我希望能够在给定索引的情况下删除整个showTimes对象。

类似showtimesList.get(index).remove()的东西如果它是arrayList中的最后一个对象,则该值应设置为null

正如其中一个答案所示,我正在将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++;
}

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

哪个是

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

3 个答案:

答案 0 :(得分:1)

这样的事情应该有效(我不是杰克逊用户,所以YMMV):

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

编辑:

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

答案 1 :(得分:1)

[('a', 'm'), ('a', 'n'), ('b', 'm'), ('b', 'n'), ('c', 'm'), ('c', 'n')]

答案 2 :(得分:0)

这样的事情应该有效

    -(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
    {

      if ([self.nav_controller.topViewController isKindOfClass:[FullVC class]])
       {
          if (indexPath.row==0)
            {
                [(FullVC *)self setDelegate:self];
            }
       }

    }
    -(void)shareToView:(NSString *)titleString inUrl:(NSString *)urlString
    {
       NSLog(@"title string after passing is: %@", titleString);
       NSLog(@"url string after passing is: %@", urlString);
    }
    @end