我的JSON响应如下:
"channels": {
"BB0AC397-78AA-41C9-818A-A52A6BC81E9B": {
"id": "BB0AC397-78AA-41C9-818A-A52A6BC81E9B",
"name": "MyVABotChannel",
},
"94612845-7868-4B1C-8E58-7BD88869B197": {
"id": "94612845-7868-4B1C-8E58-7BD88869B197",
"name": "System_Bot_Test",
}
我的要求是提取名称为MyVABotChannel的结构的id。当它们来自服务器时,id不是固定的和随机的。那么如何找到与特定名称对应的id。
答案 0 :(得分:1)
假设javascript
此代码段假定使用" MyVABotChannel"
多个条目// response = JSON
var values = Object.values(response.channels);
var botChannels = values.filter(channel => channel.name === "MyVABotChannel");
此代码段假设一个条目为" MyVABotChannel"
// response = JSON
var values = Object.values(response.channels);
var botChannels = values.find(channel => channel.name === "MyVABotChannel");
答案 1 :(得分:0)
如果它在JavaScript或任何公开JSON DOM的库中,那么就像这样:
var channels = ...
var key = Object
.getKeys( channels )
.find( e => chanels[e].name == "MyVABotChannel" );
if( key == null ) {
console.log("Couldn't find element.");
return;
}
var result = channels[key].id;
console.log( result );