Javascript 如何获取json对象中所有第二个选项的所有名称部分
这是我想从中获取的完整对象:Javascript 如何获取json对象中所有第二个选项的所有名称部分,javascript,node.js,json,Javascript,Node.js,Json,这是我想从中获取的完整对象: { "name": "permissions", "description": "Get or edit permissions for a user or a role", "options": [ { "name": "user", &q
{
"name": "permissions",
"description": "Get or edit permissions for a user or a role",
"options": [
{
"name": "user",
"description": "Get or edit permissions for a user",
"type": 2,
"options": [
{
"name": "get",
"description": "Get permissions for a user",
"type": 1,
"options": [
{
"name": "user",
"description": "The user to get",
"type": 6,
"required": true
},
{
"name": "channel",
"description": "The channel permissions to get. If omitted, the guild permissions will be returned",
"type": 7,
"required": false
}
]
},
{
"name": "edit",
"description": "Edit permissions for a user",
"type": 1,
"options": [
{
"name": "user",
"description": "The user to edit",
"type": 6,
"required": true
},
{
"name": "channel",
"description": "The channel permissions to edit. If omitted, the guild permissions will be edited",
"type": 7,
"required": false
}
]
}
]
},
{
"name": "role",
"description": "Get or edit permissions for a role",
"type": 2,
"options": [
{
"name": "get",
"description": "Get permissions for a role",
"type": 1,
"options": [
{
"name": "role",
"description": "The role to get",
"type": 8,
"required": true
},
{
"name": "channel",
"description": "The channel permissions to get. If omitted, the guild permissions will be returned",
"type": 7,
"required": false
}
]
},
{
"name": "edit",
"description": "Edit permissions for a role",
"type": 1,
"options": [
{
"name": "role",
"description": "The role to edit",
"type": 8,
"required": true
},
{
"name": "channel",
"description": "The channel permissions to edit. If omitted, the guild permissions will be edited",
"type": 7,
"required": false
}
]
}
]
}
]
}
我想从第一个json对象获取所有这些名称属性,并将它们全部分配给javascript中的不同变量?我怎样才能做到这一点
权限-->role-->edit-->channel(换句话说,我想获取json对象中所有第二个选项中的所有名称部分)如果您知道json结构是固定的,那么这将很容易 假设您的json已分配给
permissionJson
,那么您可以将其编写为
permissionJson.name // permissions
permissionJson.options[1].name. // role
permissionJson.options[1].options[1].name // edit
permissionJson.options[1].options[1].options[1].name // channel
如果JSON结构是动态的,那么您可能需要在每个级别提取options数组index 1元素并查找其名称。首先,我想澄清一下,我不太理解您的问题。如果我误解了什么,请纠正我。 您说过要获取选项(根1)数组的所有第二个成员中的所有名称部分。所以你要做的就是
JSON_DATA.options[1]
这里您选择了整个角色
对象。若要更深入,可以使用点运算符。例如权限-->角色-->编辑-->频道。这可以通过此行JSON\u数据访问。选项[1]。选项[1]
它会退回这个
{
"name": "channel",
"description": "The channel permissions to get. If omitted, the guild permissions will be returned",
"type": 7,
"required": false
}
此解决方案适用于对象将来可能达到的深度 这是您的解决方案:只需使用此函数,并创建变量
allSecondNames
:
const allSecondNames = [];
func = a => {
if (a.name) allSecondNames.push(a.name);
if (a.options) func(a.options)
if (a[1] && a[1].name) allSecondNames.push(a[1].name);
if (a[1]?.options) func(a[1].options)
}
["permissions", "role", "edit", "channel"]
将JSON发送到函数func
将放入变量allSecondNames
:
const allSecondNames = [];
func = a => {
if (a.name) allSecondNames.push(a.name);
if (a.options) func(a.options)
if (a[1] && a[1].name) allSecondNames.push(a[1].name);
if (a[1]?.options) func(a[1].options)
}
["permissions", "role", "edit", "channel"]
您好,什么是“选择”?你能给出更多的解释,或者你希望从这个json中得到的输出吗?到目前为止,你自己尝试过解决这个问题吗?“假设你的json被分配给
permissionJson
,那么你可以将它写为“-不,因为必须首先解析它