0

I have two JSON files and one references the other.

The first file below is parsed into the program as moves, it's referencing into a spritesheet.

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

Then I define another object dong as a key value pair like below

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

And can reference the data as doing.frame[0].sprite.x and all is good.

My problems start when I try to make the doing object as a JSON file as it requires the value sprite to be a string and not an object reference.

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

Is there a way to define an object reference for JSON or a way to convert the string "moves.stand" back into an object reference?

I have managed to use a single word string reference but not a dot syntax reference. But not with the dot notation.

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


 moves[doing.frame[0].sprite].x
Hellonearthis
  • 1,664
  • 1
  • 18
  • 26

1 Answers1

2

You can save JSON.stringify(doing) content to your file.

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));
Hien Nguyen
  • 24,551
  • 7
  • 52
  • 62
  • Thanks, that works but I lose readability that the object reference provides as the moes.stand is replaced with "x": 0, "y": 12, "width": 49, "height": 52 data – Hellonearthis May 06 '19 at 03:49
  • you can compare with your moves.stand after you parse file to object by JSON.parse – Hien Nguyen May 06 '19 at 03:51
  • It does help with read ability. If I have "stand" instead "moves.stand" then I can moves[doing.frame[0].sprite].x as the string can be a reference. But not with "moves.stand" and [doing.frame[0].sprite].x The reason to not have the two json files combine is I only need one change in the moves file and it will be updated vi all references and integration would mean a heap of editing. Still thanks as it's help me work out a kind of fix with single word string references. – Hellonearthis May 06 '19 at 04:54