解析节点js

时间:2018-04-17 11:54:00

标签: javascript node.js

我有一个javascript文件,其中包含一个对象数组。该数组还包含一些作为对象的函数。所以请记住,我不能使用JSON解析,因为它会返回错误。现在我需要读取该js文件并访问该数组,并在该数组中添加另一个函数作为对象并将其保存到硬盘中。有没有可以帮助我解决这个问题的解析库?

这是js文件

let notifications = {
"Welcome": {
    title: "Welcome",
    body: "",
    icon: ""
}};

let script = {
"Start": [
    "notify Welcome",
    {
        "Input": {
            "Text": "What is your name?",
            "Validation": function (input) {
                return input.trim().length > 0;
            },
            "Save": function (input) {
                storage.player.Name = input;
                return true;
            },
            "Warning": "You must enter a name!"
        }
    },

    "h Hi {{player.Name}} Welcome!",

    "h Currently you have {{player.intelligence}} points of Intelligence but you seem far more intelligent, how about we add five points?",
    {"Function":{
        "Apply": function () {
            storage.player.intelligence += 5;
            return true;
        },

        "Reverse": function () {
            storage.player.intelligence -= 5;
        }
    }},
    "h There you have it, you now have {{player.intelligence}} points of Intelligence",

    {
        "Choice": {
            "Dialog": "h Have you already read some documentation?",
            "Yes": {
                "Text": "Yes",
                "Do": "jump Yes"
            },
            "No": {
                "Text": "No",
                "Do": "jump No"
            }
        }
    }
],

"Yes": [

    "h That's awesome!",
    "h Then you are ready to go ahead and create an amazing Game!",
    "h I can't wait to see what story you'll tell!",
    "end"
],

"No": [

    "h You can do it now.",

    "display message Help",

    "h Go ahead and create an amazing Game!",
    "h I can't wait to see what story you'll tell!",
    "end"
]};

现在我需要以编程方式在脚本数组中添加更多选项并保存。

1 个答案:

答案 0 :(得分:1)

你应该看看JSON-fn。它可用于执行以下操作:

  

Javascript(都是node.js和浏览器)插件,用

字符串化/解析/克隆jsvascript对象      
      
  • 功能
  •   
  • 正则表达式
  •   
  • 日期
  •