我需要将一些数据转换成JSON格式,以便在我的React.js项目中使用这些数据。
目前的数据格式如下:
["'Your Cut'"] = {
["id"] = 3400,
["tier"] = 1,
["type"] = {"Distributed"},
["limit"] = "Only granted by {{cai|Death from Below|Pyke}}.",
["maps"] = {
["sr"] = true,
["ha"] = true,
["nb"] = true,
["tt"] = true,
["cs"] = false,
},
["effects"] = {
["consume"] = "Grants {{g|text=*none*}}{{pp|100;112;140;176;220;274;300|-7+x;0+|type=target's kill bounty|color=gold}}, increased by {{g|100}} for [[First Blood]]. '''Can be used while [[dead]].'''",
},
},
["Abyssal Mask"] = {
["id"] = 8020,
["tier"] = 3,
["maps"] = {
["sr"] = true,
["ha"] = true,
["nb"] = true,
},
["menu"] = {
["tank"] = true,
},
["stats"] = {
["ah"] = 10,
["hp"] = 450,
["mr"] = 35,
},
["effects"] = {
["pass"] = {
["name"] = "Unmake",
["unique"] = true,
["description"] = "Enemy champions within {{tt|550 units|center to edge}} of you become cursed, reducing their {{as|magic resistance by 5}} {{as|(+ {{fd|1.2}}% '''bonus''' health)}}, capped at a reduction of {{as|25 magic resistance}}. Gain {{as|9 '''bonus''' magic resistance}} per cursed enemy.",
},
},
["recipe"] = {"Kindlegem", "Spectre's Cowl"},
["buy"] = 2700,
},
["Aegis of the Legion"] = {
["id"] = 3105,
["tier"] = 2,
["maps"] = {
["sr"] = true,
["ha"] = true,
["nb"] = true,
},
["menu"] = {
["tank"] = true,
["support"] = true,
},
["stats"] = {
["ah"] = 10,
["armor"] = 30,
["mr"] = 30,
},
["recipe"] = {"Null-Magic Mantle", "Cloth Armor"},
["buy"] = 1400,
},..。再等6000条线路。
我尝试过在中使用REGEX替换,但是它停止了响应并使程序崩溃。
什么是正确的方法来处理这个问题?
我应该用某种语言编写一个脚本来转换数据吗?如果是这样的话,我应该使用REGEX还是有更聪明的方法?
谢谢
发布于 2022-08-09 15:46:59
您需要知道输入的确切语法规则。例如,如果字符串文本可能包括换行符或双引号(分隔符)等特殊字符,以及如何编码。
但是,对于给出的示例,我看到需要执行以下转换才能使其有效的JSON:
键名周围的方括号应该去掉braces
。
看到输入结构,我做了几个假设,包括:
在单行上打开和关闭
F 221
下面是JavaScript中该转换的一个可运行的实现,它为您提供的示例输入生成一个结果:
function toJSON(input) {
return ("{\n"
+ input.replace(/^(\s*)\[("[^"]*")](\s*)=/gm, "$1$2$3:")
.replace(/\{(.*)}(,?)[ \t]*$/gm, "[$1]$2")
+ "\n}").replace(/,(\s*\})/g, "$1");
}
// Demo on the example:
let input = `["'Your Cut'"] = {
["id"] = 3400,
["tier"] = 1,
["type"] = {"Distributed"},
["limit"] = "Only granted by {{cai|Death from Below|Pyke}}.",
["maps"] = {
["sr"] = true,
["ha"] = true,
["nb"] = true,
["tt"] = true,
["cs"] = false,
},
["effects"] = {
["consume"] = "Grants {{g|text=*none*}}{{pp|100;112;140;176;220;274;300|-7+x;0+|type=target's kill bounty|color=gold}}, increased by {{g|100}} for [[First Blood]]. '''Can be used while [[dead]].'''",
},
},
["Abyssal Mask"] = {
["id"] = 8020,
["tier"] = 3,
["maps"] = {
["sr"] = true,
["ha"] = true,
["nb"] = true,
},
["menu"] = {
["tank"] = true,
},
["stats"] = {
["ah"] = 10,
["hp"] = 450,
["mr"] = 35,
},
["effects"] = {
["pass"] = {
["name"] = "Unmake",
["unique"] = true,
["description"] = "Enemy champions within {{tt|550 units|center to edge}} of you become cursed, reducing their {{as|magic resistance by 5}} {{as|(+ {{fd|1.2}}% '''bonus''' health)}}, capped at a reduction of {{as|25 magic resistance}}. Gain {{as|9 '''bonus''' magic resistance}} per cursed enemy.",
},
},
["recipe"] = {"Kindlegem", "Spectre's Cowl"},
["buy"] = 2700,
},
["Aegis of the Legion"] = {
["id"] = 3105,
["tier"] = 2,
["maps"] = {
["sr"] = true,
["ha"] = true,
["nb"] = true,
},
["menu"] = {
["tank"] = true,
["support"] = true,
},
["stats"] = {
["ah"] = 10,
["armor"] = 30,
["mr"] = 30,
},
["recipe"] = {"Null-Magic Mantle", "Cloth Armor"},
["buy"] = 1400,
},`;
const json = toJSON(input);
console.log(JSON.parse(json));
https://stackoverflow.com/questions/73294208
复制相似问题