访问引用另一个JSONS密钥的JSON值



我有两个JSON文件和一个引用。

下面的第一个文件以 moves 为单位,将其引用到spritesheet中。

{"stand": { "x":  0, "y": 12, "width": 49, "height": 52 },
 "walk1": { "x": 52, "y": 12, "width": 50, "height": 52 }}

然后,我将另一个对象定义为为键值对,如下

doing = { frame: [{sprite:moves.stand, xoff:  0, yoff:102},
                  {sprite:moves.walk, xoff: 10, yoff:102} ]}

可以将数据引用为 do.frame [0] .sprite.x ,一切都很好。

当我尝试将对象作为JSON文件时,我的问题就开始了,因为它要求值sprite是字符串而不是对象引用。

{frame:[{"sprite":"moves.stand", "xoff":  0, "yoff":102},
         "sprite":"moves.stand", "xoff": 10, "yoff":122}]}

是否有一种方法可以定义JSON或一种将字符串" moves.studd"转换回对象引用的方法?

我设法使用了单词字符串引用,但不使用点语法引用。但是不是点表示法。

{frame:[{"sprite":"stand", "xoff": 0, "yoff":102},
        {"sprite":"walk0", "xoff": 64, "yoff":102}]}

 moves[doing.frame[0].sprite].x

您可以将JSON.stringify(doing)内容保存到文件中。

var moves = {"stand": { "x": 0, "y": 12, "width": 49, "height": 52 }}
var doing = {
    "frame": [{"sprite":moves.stand, "xoff":  0, "yoff":102},
            {"sprite":moves.stand, "xoff": 10, "yoff":122} ]
}  
console.log(JSON.stringify(doing));

最新更新