Question

I understand I can insert new properties into JSON like so:

var jsonObj = {
    members: 
           {
            host: "hostName",
            viewers: 
            {
                user1: "value1",
                user2: "value2",
                user3: "value3"
            }
        }
}

jsonObj.members.viewers["user4"] = "value4"; 

But the JSON I need to work with is like so (from Texture Packer):

var jsonObj = {
    "frames": {
        "chaingun.png": {
            "frame": {
                "x": 1766,
                "y": 202,
                "w": 42,
                "h": 34
            },
            "rotated": false,
            "trimmed": true,
            "spriteSourceSize": {
                "x": 38,
                "y": 32,
                "w": 42,
                "h": 34
            },
            "sourceSize": {
                "w": 128,
                "h": 128
            }
        }        
    }
}

How would I insert a new property into "frame"? It's confusing me because of the speech marks.

Était-ce utile?

La solution

You can simply chain the members like so:

jsonObj.frames["chaingun.png"]["frame"]["z"] = 1234;

// or

jsonObj.frames["chaingun.png"].frame.z = 4567;
Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top