首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >将数据转换为JSON格式的最快方法是什么?

将数据转换为JSON格式的最快方法是什么?
EN

Stack Overflow用户
提问于 2022-08-09 15:15:29
回答 1查看 51关注 0票数 0

我需要将一些数据转换成JSON格式,以便在我的React.js项目中使用这些数据。

目前的数据格式如下:

代码语言:javascript
复制
["'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还是有更聪明的方法?

谢谢

EN

回答 1

Stack Overflow用户

发布于 2022-08-09 15:46:59

您需要知道输入的确切语法规则。例如,如果字符串文本可能包括换行符或双引号(分隔符)等特殊字符,以及如何编码。

但是,对于给出的示例,我看到需要执行以下转换才能使其有效的JSON:

键名周围的方括号应该去掉braces

  • Trailing

  • ,终止数组的大括号应该转换为方括号

  • ,整个输入应该用一对花括号(在对象文字的最后一个键之后)包装起来,应该删除

看到输入结构,我做了几个假设,包括:

在单行上打开和关闭

  • 数组;
  • 数组不是在单独的行上定义;
  • 键名和字符串值总是双引号。
  • 不执行字符串内插(双大括号保持不变)

F 221

下面是JavaScript中该转换的一个可运行的实现,它为您提供的示例输入生成一个结果:

代码语言: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));

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/73294208

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档