gpt4 book ai didi

javascript - 如何从 JSON OBJECT 获取内部数据

转载 作者:行者123 更新时间:2023-12-02 19:01:56 25 4
gpt4 key购买 nike

请注意,这是一个实际的 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);

最佳答案

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

'x' 也需要引号。

你也可以这样做:

parsedjson.frames['chaingun_impact.png'].spriteSourceSize.x;

关于javascript - 如何从 JSON OBJECT 获取内部数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14722011/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com