0

I have this JSON file in this format;

[{
"item-id" : "0",
"item-name" : "Dwarf remains",
"item-examine" : "The body of a Dwarf savaged by Goblins.",
"shop-value" : "0.0",
"low-alch" : "0.0",
"high-alch" : "0.0",
"stab-bonus" : "0",
"slash-bonus" : "0",
"crush-bonus" : "0",
"magic-bonus" : "0",
"ranged-bonus" : "0",
"stab-defence" : "0",
"slash-defence" : "0",
"crush-defence" : "0",
"magic-defence" : "0",
"ranged-defence" : "0",
"strength-bonus" : "0",
"prayer-bonus" : "0",
},
{
"item-id" : "1",
"item-name" : "Toolkit",
"item-examine" : "Good for repairing a broken cannon.",
"shop-value" : "0.0",
"low-alch" : "0.0",
"high-alch" : "0.0",
"stab-bonus" : "0",
"slash-bonus" : "0",
"crush-bonus" : "0",
"magic-bonus" : "0",
"ranged-bonus" : "0",
"stab-defence" : "0",
"slash-defence" : "0",
"crush-defence" : "0",
"magic-defence" : "0",
"ranged-defence" : "0",
"strength-bonus" : "0",
"prayer-bonus" : "0",
}]

But with 25 000 entries

But in the file there is something wrong;

"prayer-bonus" : "0",

should be

"prayer-bonus" : "0"

How can i use regex or whatever to only replace the last bit?

Im using java.

By the way: the value is not always "0"

So how can i have it be a wildcard so it replaces all ?

4

4 回答 4

0

You can use this (written in java style, to use with replaceAll() method):

search: (?<!\\\\)(\"(?>[^\\\"]++|\\\\{2}|\\\\.)*+\"),(\\s*+})
replace: $1$2

The main interest of this pattern is that you avoid escaped double quotes that may be inside quotes.

Example:

String pattern = "(?<!\\\\)(\"(?>[^\\\"]++|\\\\{2}|\\\\.)*+\"),(\\s*+})";
String result = yourJson.replaceAll(pattern, "$1$2");
于 2013-11-13T16:44:29.943 回答
0

使用 sed:

sed -i.bak 's/\("prayer-bonus" : "0"\),/\1/' file

要匹配任何数字:

sed -i.bak 's/\("prayer-bonus" : "[0-9]*"\),/\1/' file
于 2013-11-13T16:37:02.873 回答
0

看起来您可能正在尝试修补由格式不正确的 JSON blob 引起的问题。您可以采用其他响应者的回答,即使用 sed 修复这些症状,但问题在于 JSON 的创建方式。

于 2013-11-13T16:39:28.210 回答
0

任何文本编辑器都可以解决问题。

在此处输入图像描述

或者您可以从 java 调用字符串替换。不需要正则表达式。

于 2013-11-13T16:40:09.803 回答