java在String.split中使用正则表达式来准备json参数

时间:2017-11-30 08:50:21

标签: java json regex

我有一个包含此类文档的csv文件:

{""cast_id"": 10, ""character"": ""Mushu (voice)"", ""credit_id"": ""52fe43a09251416c75017cbb"", ""gender"": 2, ""id"": 776, ""name"": ""Eddie Murphy"", ""order"": 0}, {""cast_id"": 62, ""character"": ""[Singing voice]"", ""credit_id"": ""597a65c8925141233d0000bb"", ""gender"": 2, ""id"": 18897, ""name"": ""Jackie Chan"", ""order"": 1}, {""cast_id"": 16, ""character"": ""Mulan (voice)"", ""credit_id"": ""52fe43a09251416c75017cd5"", ""gender"": 1, ""id"": 21702, ""name"": ""Ming-Na Wen"", ""order"": 2}

我首先使用此正则表达式将四重引号更改为双引号:

String newResult = result.replaceAll("\"{2}", "\"");

然后我使用这个正则表达式来分割这个字符串:

String[] jsonResult = newResult.split(", (?![^{]*\\})");

然而,它将字符串分隔为:

{"cast_id": 10, "character": "Mushu (voice)", "credit_id": "52fe43a09251416c75017cbb", "gender": 2, "id": 776, "name": "Eddie Murphy", "order": 0}

{"cast_id": 62

"character": "[Singing voice

然后

{"cast_id": 16, "character": "Mulan (voice)", "credit_id": "52fe43a09251416c75017cd5", "gender": 1, "id": 21702, "name": "Ming-Na Wen", "order": 2}

所以我的正则表达式在遇到方括号[]时失败了,我可以帮忙解决这个问题吗?

我尝试使用http://www.regexplanet.com/advanced/java/index.html,但我不明白我应该在选项,替换和输入中添加什么。我如何使用本网站?

由于

3 个答案:

答案 0 :(得分:1)

您正在处理已保存为一个列CSV文件的JSON数据。 :) 报价将使用CSV双引号进行转义,因此您只需使用CSV库即可读取文件。正如我所说,你应该只得到一列 - 一个包含你的JSON的值。然后使用JSON库来解析JSON。

=>你根本不需要实现任何解析。

答案 1 :(得分:0)

你应该寻找模式}, { 正则表达式:(?<=\}), (?=\{)就是这么做的。如果字符串末尾缺少},你的正则表达式会给出误报。

(用https://regex101.com/测试)

之后,您可以将每个字符串解析为JSON,为此使用库。

答案 2 :(得分:0)

正如其他人所推荐的那样,解析器比分裂自己更好。例如,当您获得嵌套括号时,正则表达式会遇到限制。我使用Google's Gson library,调整输入会略微产生所需的分割。重要的一步是将输入转换为JSON数组,否则解析器将在第一个元素后失败:

// Pre-processed your input to remove the double double quotes
String input = "{'cast_id': 10, 'character': 'Mushu (voice)', 'credit_id': '52fe43a09251416c75017cbb', 'gender': 2, 'id': 776, 'name': 'Eddie Murphy', 'order': 0}, {'cast_id': 62, 'character': '[Singing voice]', 'credit_id': '597a65c8925141233d0000bb', 'gender': 2, 'id': 18897, 'name': 'Jackie Chan', 'order': 1}, {'cast_id': 16, 'character': 'Mulan (voice)', 'credit_id': '52fe43a09251416c75017cd5', 'gender': 1, 'id': 21702, 'name': 'Ming-Na Wen', 'order': 2}";

JsonArray array = new JsonParser().parse("[" + input + "]").getAsJsonArray();
for (int i = 0; i < array.size(); i++)
{
    System.out.println(array.get(i));
}

输出:

{"cast_id":10,"character":"Mushu (voice)","credit_id":"52fe43a09251416c75017cbb","gender":2,"id":776,"name":"Eddie Murphy","order":0}
{"cast_id":62,"character":"[Singing voice]","credit_id":"597a65c8925141233d0000bb","gender":2,"id":18897,"name":"Jackie Chan","order":1}
{"cast_id":16,"character":"Mulan (voice)","credit_id":"52fe43a09251416c75017cd5","gender":1,"id":21702,"name":"Ming-Na Wen","order":2}