我有来自Atlassian Jira的未分类的JSON数据,该数据是我通过GET-request获得的:
[
{
"id":"customfield_10000",
"name":"Konzeption",
"custom":true,
"orderable":true,
"navigable":true,
"searchable":true,
"clauseNames":[
"cf[10000]",
"Konzeption"
],
"schema":{
"type":"number",
"custom":"com.atlassian.jira.plugin.system.customfieldtypes:float",
"customId":10000
}
},
{
"id":"priority",
"name":"Priority",
"custom":false,
"orderable":true,
"navigable":true,
"searchable":true,
"clauseNames":[
"priority"
],
"schema":{
"type":"priority",
"system":"priority"
}
},
{
"id":"customfield_10001",
"name":"Umsetzung",
"custom":true,
"orderable":true,
"navigable":true,
"searchable":true,
"clauseNames":[
"cf[10001]",
"Umsetzung"
],
"schema":{
"type":"number",
"custom":"com.atlassian.jira.plugin.system.customfieldtypes:float",
"customId":10001
}
}
]
我只想拥有每个“ customfield _ #####”的“ id”。如何搜索JSON以获取每个自定义字段的每个ID?
答案 0 :(得分:1)
您可以使用forEach(function)枚举数组中的每个子节点! //假设您的数据在一个数组中(正如您所提到的!) yourData.forEach(processEachNodes); //由于ForEach是数组的函数,因此您只能 //如果您的数据是数组,请使用它,幸运的是,它是
function processEachNodes(myNode, index, rawObject){
if(myNode.id.indexOf("customfield_") === 0){
//DO SOMETHING HERE
}
}
在获得子节点之后,无论您想要什么条件,您都可能知道如何处理这些ID。
答案 1 :(得分:1)
因为您尚未定义编程语言,所以我会用不同的语言为她提供一些示例,希望对您有所帮助。
第一个PHP:
<?php
$j ='[
{
"id":"customfield_10000",
"name":"Konzeption",
"custom":true,
"orderable":true,
"navigable":true,
"searchable":true,
"clauseNames":[
"cf[10000]",
"Konzeption"
],
"schema":{
"type":"number",
"custom":"com.atlassian.jira.plugin.system.customfieldtypes:float",
"customId":10000
}
},
{
"id":"priority",
"name":"Priority",
"custom":false,
"orderable":true,
"navigable":true,
"searchable":true,
"clauseNames":[
"priority"
],
"schema":{
"type":"priority",
"system":"priority"
}
},
{
"id":"customfield_10001",
"name":"Umsetzung",
"custom":true,
"orderable":true,
"navigable":true,
"searchable":true,
"clauseNames":[
"cf[10001]",
"Umsetzung"
],
"schema":{
"type":"number",
"custom":"com.atlassian.jira.plugin.system.customfieldtypes:float",
"customId":10001
}
}
]';
$j = json_decode($j);
foreach($j as $o){
echo (strpos($o->id, "customfield_")!==false)?'ok':'none';
}
现在的Javascript:
var j = '[{"id":"customfield_10000","name":"Konzeption","custom":true,"orderable":true,"navigable":true,"searchable":true,"clauseNames":["cf[10000]","Konzeption"],"schema":{"type":"number","custom":"com.atlassian.jira.plugin.system.customfieldtypes:float","customId":10000}},{"id":"priority","name":"Priority","custom":false,"orderable":true,"navigable":true,"searchable":true,"clauseNames":["priority"],"schema":{"type":"priority","system":"priority"}}, {"id":"customfield_10001","name":"Umsetzung","custom":true,"orderable":true,"navigable":true,"searchable":true,"clauseNames":["cf[10001]","Umsetzung"],"schema":{"type":"number","custom":"com.atlassian.jira.plugin.system.customfieldtypes:float","customId":10001}}]';
j = JSON.parse(j);
for(o in j){
console.log ((j[o].id.indexOf("customfield_")>=0)?'ok':'none');
}
如果您需要更多示例,请随意发表评论。