解析JSON在Javascript中创建对象。

时间:2021-08-28 01:21:41

Maybe this was already discussed somewhere, I myself could not find an exact answer how to approach my problem:

也许这已经在什么地方讨论过了,我自己找不到一个确切的答案来解决我的问题:

I have a mutliniear «story» which executes code in each segment. I wrote a state machine, which initiates a new segment, whenever another segment calls that one. Each segment has an onEnter-, an onCheck and an onLeave function. As the name already says, the onEnter executes when the segment is called, the onCheck checks some input until some conditions are fullfilled (if yes they will lead to another segment) and onLeave executes just before the next segment is called.

我有一个mutliniear«story»,它在每个段中执行代码。我编写了一个状态机,每当另一个段调用它时,它就启动一个新的段。每个段都有一个onEnter-、一个onCheck和一个onLeave函数。顾名思义,onEnter在调用段时执行,onCheck检查一些输入,直到某些条件被填满(如果是,它们将导致另一个段),onLeave在调用下一个段之前执行。

Currently I just wrote a javascript object kinda like this:

目前我刚写了一个javascript对象

var flow = {
    seg1: {
        onEnter: function() {
            this.say('Seg1');
        },
        onCheck: function(data) {
            if (data.condition) {
                machine.callNextSeg('seg2');
            } else if (data.condition2) {
                machine.callNextSeg('seg3');
            }
        },
        onLeave: function() {
        }
    },
    seg2: {
        onEnter: function() {
            this.say('Seg2');
        },
        onGestureCheck: function(data) {
        },
        onLeave: function() {
        }
    },
    seg3: {
        onEnter: function() {
            this.say('Seg3');
        },
        onGestureCheck: function(data) {

        },
        onLeave: function() {
        }
    }
};

The example is a bit simplified for understanding, but the code inside would be a little more complex.

为了便于理解,这个示例稍微简化了一些,但是里面的代码会稍微复杂一些。

I would rather like to have a JSON file, which is loaded, parsed and creates such an object. The JSON File should use a more simple and more abstract syntax to write. Especially to make it quicker.

我希望有一个JSON文件,它被加载、解析并创建这样一个对象。JSON文件应该使用更简单、更抽象的语法来编写。尤其是为了更快。

I imagine something like this:

我想象的是这样的:

{
    "seg1": {
        "onEnter": {
            "say": 'Seg1'
        },
        "onCheck": {
            "condition1": "seg2",
            "condition2": "seg3"
        },
        "onLeave": {

        }
    },
    "seg2": {
        "onEnter": {
            "say": 'Seg2'
        },
        "onCheck": {

        },
        "onLeave": {

        }
    }
}

The conditions are booleans and if true the described segment should be called.

条件为布尔值,如果为真,则应调用所描述的段。

Would that be easy to parse the json, create an object, and create functions inside it for each onEnter, onCheck and onLeave? And also write the necessary if clauses?

解析json、创建对象、为每个onEnter、onCheck和onLeave在其中创建函数都很容易吗?还要写必要的if从句?

Thanks for hints into the right direction.

谢谢你给我的建议。

cheers

干杯

J.

J。

1 个解决方案

#1


1  

var flow = {...} is an object literal and is actually best suited for what you need to do, but I do understand the desire to have all of this "defined" in a JSON file. The issue there becomes the fact that the application processing your JSON file MUST understand/be Javascript (which defies the idea of having JSON as language agnostic - its name notwithstanding). This might be what you need: JavaScript Function Serialization

var流= {…}是一个对象文本,实际上最适合您需要做的事情,但我确实理解在JSON文件中定义所有这些“定义”的愿望。这里的问题变成了这样一个事实:处理JSON文件的应用程序必须理解/be Javascript(这违背了将JSON作为语言不可知论者的想法——尽管是它的名字)。这可能是您需要的:JavaScript函数序列化

Also check the voted answer here: What is the correct way to "serialize" functions in javascript for later use

还可以在这里检查投票的答案:在javascript中“序列化”函数的正确方法是什么

#1


1  

var flow = {...} is an object literal and is actually best suited for what you need to do, but I do understand the desire to have all of this "defined" in a JSON file. The issue there becomes the fact that the application processing your JSON file MUST understand/be Javascript (which defies the idea of having JSON as language agnostic - its name notwithstanding). This might be what you need: JavaScript Function Serialization

var流= {…}是一个对象文本,实际上最适合您需要做的事情,但我确实理解在JSON文件中定义所有这些“定义”的愿望。这里的问题变成了这样一个事实:处理JSON文件的应用程序必须理解/be Javascript(这违背了将JSON作为语言不可知论者的想法——尽管是它的名字)。这可能是您需要的:JavaScript函数序列化

Also check the voted answer here: What is the correct way to "serialize" functions in javascript for later use

还可以在这里检查投票的答案:在javascript中“序列化”函数的正确方法是什么