RegEx模式中的异常

时间:2013-12-03 08:59:11

标签: java regex json

我想解析JSON,如:

{"response":[9816,{"vid":166941761,"owner_id":-460389,"title":"Хочу         спиться!","description":"Вкусная группа БОРЩ - http:\/\/vk.com\/borsch<br\/><br\/>","duration":45,"link":"video-460389_166941761","image":"http:\/\/cs322230.vk.me\/u56348533\/video\/m_ddba3f2b.jpg","image_medium":"http:\/\/cs322230.vk.me\/u56348533\/video\/l_809f4d95.jpg","date":1385374022,"views":20981,"comments":135,"player":"http:\/\/www.youtube.com\/embed\/4Ik-rjxf3dw"},{"vid":166474172,"owner_id":-460389,"title":"Скороговорка от Джигурды для Борща","description":"Вкусная гру..

我需要删除第一个重新预测总视频数量的元素。我正在使用

json = json.replaceAll(".*[0-9],{", "");
json = "{\"response\":[" + json;

adn getting:

 12-03 08:49:10.964: E/AndroidRuntime(2445): java.util.regex.PatternSyntaxException:         Error in {min,max} interval near index 9:
 12-03 08:49:10.964: E/AndroidRuntime(2445): .*[0-9],{

2 个答案:

答案 0 :(得分:0)

逃离最后{,即:

json = json.replaceAll(".*[0-9],\\{", "");

但实际上,真的,使用正确的JSON解析器!

答案 1 :(得分:0)

Nikos说:逃避{

但看起来你当前的方法会留下无效的json。您正在移除{,但结束}仍然存在,因此您需要将{添加回替换部分:

json = json.replaceAll(".*[0-9],{", "{\"response\":[{");
                                                    ^

但是,对于您的示例数据,您可以使用

json = json.replaceAll("\\[[0-9]+", "[");