0

これは実際のJavascriptオブジェクトであるのに対し、JSONはそのオブジェクトを表す文字列であることに注意してください。

JSONExample = {
"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
        }
    },
    "chaingun_impact.png": {
        "frame": {
            "x":1162,
            "y":322,
            "w":38,
            "h":34},
        "rotated": false,
        "trimmed": true,
        "spriteSourceSize": {
            "x":110,
            "y":111,
            "w":38,
            "h":34},
        "sourceSize": {
            "w":256,
            "h":256}
    },
    "chaingun_impact_0000.png": {
        "frame": {
            "x": 494,
            "y": 260,
            "w": 22,
            "h": 22
        },
        "rotated": false,
        "trimmed": true,
        "spriteSourceSize": {
            "x": 113,
            "y": 108,
            "w": 22,
            "h": 22
        },
        "sourceSize": {
            "w": 256,
            "h": 256
        }
    }
}
};

上記は、JSONがどのように構造化されるかの例です。chaingun_impact.pngはここにはないことに注意してください。完全なJSON入力を使用してparseJSON関数を呼び出します。

parseJSON = function (weaponJSON) {
 // Your Code here 

};

'chaingun_impact.png'の'spriteSourceSize'内の'x'データフィールドを取得したい。

前進してくれてありがとう

編集

これは私が試したものですが、機能しません

var parsedjson =  JSON.parse(weaponJSON);
alert(parsedjson);
console.log(pardesjson);
var x = parsedjson ['frames']['chaingun_impact.png']['spriteSourceSize'][x];
console.log(x);
4

1 に答える 1

2

使用するparsedjson['frames']['chaingun_impact.png']['spriteSourceSize']['x'];

'x'引用符も必要です。

次のようにすることもできます。

parsedjson.frames['chaingun_impact.png'].spriteSourceSize.x;
于 2013-02-06T05:13:22.933 に答える