我如何删除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字符串中删除showTimes
的第二个元素时,它会抛出错误Exception in thread "main" java.lang.IllegalStateException
at java.util.ArrayList$Itr.remove(Unknown Source)
这就是问题所在。
{
"date":"01/12/15",
"time":"23:30"
}