Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/json/13.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
java使用String.split中的正则表达式准备json参数_Java_Json_Regex - Fatal编程技术网

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

java使用String.split中的正则表达式准备json参数,java,json,regex,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]"", ""cre

我有一个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}
所以我的正则表达式在遇到方括号[]时失败了,我能得到一些帮助吗

我试图使用,但我不明白我应该在选项、替换和输入中输入什么。我如何使用这个网站


谢谢

您正在处理JSON数据,该数据已保存为一列CSV文件。:) 引号将在CSV中用双引号转义,因此您可以使用CSV库读取文件。正如我所说,您应该期望只得到一列——一个包含JSON的值。然后使用JSON库解析JSON


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

您应该查找模式
},{

正则表达式:
(?正如其他人所建议的,解析器将是一个比自己拆分更好的解决方案。例如,当您使用嵌套括号时,正则表达式会受到限制。我使用了,并稍微调整您的输入以产生所需的拆分。重要的一步是将您的输入转换为JSON数组,否则在fir之后解析器将失败st元素:

// 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));
}

我建议您使用JSON解析器,而不是正则表达式。这将为您节省大量开销。我尝试了JSON simple,但JSON simple只接受标准JSON类型。这就是为什么我要将字符串更改为单个标准JSON字符串,然后我会对其进行解析。JSON simple或其他包中是否有方法拆分mul字符串tiple json input以逗号分隔?我在json SimpleX中找不到它。你所说的“标准json类型”是什么意思,就像他的例子中的那些。我的数据是由几个json组成的字符串,以逗号分隔。这就是为什么我需要将它们分开,然后解析它们。你所说的“引号将在CSV中用双引号转义”是什么意思?还有一种方法可以自动读取csv文件或字符串吗?csv规范()的一部分文本数据通常用引号括起来,如果您想在文本中使用引号(如JSON测试),可以写一个双引号。如果我是您,我会尝试先用这样的东西()然后再解析JSON。第一步谷歌:“Java读取CSV库”第二步谷歌:“Java解析JSON”我想我明白你的意思了。你能告诉你如何在eclipse上安装公共CSV库吗?我知道它不是jar文件。谷歌“apache commons CSV jar”=>
{"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}