gpt4 book ai didi

javascript - 浏览复杂的 JSON

转载 作者:行者123 更新时间:2023-11-29 17:56:11 25 4
gpt4 key购买 nike

首先,请原谅我的英语不好,我是法国人。

我来找你是因为我有问题。我想在 Javascript 中使用循环浏览复杂的 JSON 对象(因为它使用 JOINTJS 自行生成),但我无法做到这一点。我可以通过 json ["cells"] ["7"] ["attrs"] ["text"] ["text"] 手动完成。这是一个元素的 JSON 示例:

{"cells":[
{
"type":"basic.Image",
"position":{
"x":50,
"y":350
},
"size":
{
"width":100,
"height":50
},
"angle":0,
"id":"4a2802a8-0bd6-4d06-9343-921092a1decd",
"z":1,
"attrs":{
"text":{
"text":"230004",
"fill":"black"
},
"image":{
"xlink:href":"/uploads/documents/computer.png",
"width":100,
"height":50
}
}
}
]}

并解析 JSON :

enter image description here

我会得到“文本”:“230004”(根据项目而变化)。

预先感谢您的帮助!

最佳答案

您可以像这样访问对象:obj.cells[7].attrs.text.text,其中 obj 是保存对象的变量。

另请注意,由于 cells 属性包含一个数组,您可以遍历该数组并分别获取每个单独的值,如下所示:

var obj = {
"cells": [{
"type": "basic.Image",
"position": {
"x": 50,
"y": 350
},
"size": {
"width": 100,
"height": 50
},
"angle": 0,
"id": "4a2802a8-0bd6-4d06-9343-921092a1decd",
"z": 1,
"attrs": {
"text": {
"text": "230004",
"fill": "black"
},
"image": {
"xlink:href": "/uploads/documents/computer.png",
"width": 100,
"height": 50
}
}
}, {
"type": "basic.Image",
"position": {
"x": 50,
"y": 350
},
"size": {
"width": 100,
"height": 50
},
"angle": 0,
"id": "4a2802a8-0bd6-4d06-9343-921092a1decd",
"z": 1,
"attrs": {
"text": {
"text": "230005",
"fill": "black"
},
"image": {
"xlink:href": "/uploads/documents/computer.png",
"width": 100,
"height": 50
}
}
}, {
"type": "basic.Image",
"position": {
"x": 50,
"y": 350
},
"size": {
"width": 100,
"height": 50
},
"angle": 0,
"id": "4a2802a8-0bd6-4d06-9343-921092a1decd",
"z": 1,
"attrs": {
"text": {
"text": "230006",
"fill": "black"
},
"image": {
"xlink:href": "/uploads/documents/computer.png",
"width": 100,
"height": 50
}
}
}]
}

obj.cells.forEach(function(cell) {
console.log(cell.attrs.text.text);
});

关于javascript - 浏览复杂的 JSON,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38891015/

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