請告訴我如何在可用的 groovy JSON 中找到根 json 元素
{
"mom": [{"height": 1, "weight": 2}],
"dad": [{"height": 2, "weight": 3}]
}
你需要找到一個高度欄位 = 2 的物件(在這種情況下,它是爸爸)并從中獲取權重值(在這種情況下,它是 3)謝謝
uj5u.com熱心網友回復:
類似的東西?我們遍歷鍵,并在值陣列中將targetkey與進行比較targetvalue。然后我們得到weight找到的專案的 。
var test = {
"mom": [{
"height": 1,
"weight": 2
}],
"dad": [{
"height": 2,
"weight": 3
}]
};
//you need to find an object whose height field = 2 (in this case, it's dad) and get the weight value from //it (in this case, it's 3) thanks
const targetkey = "height";
const targetvalue = 2;
const neededkey = "weight";
let res = null;
for (const key in test) {
let val = test[key][0];
if (val[targetkey] == targetvalue) {
console.log("match :", key);
res = val[neededkey];
}
}
console.log(neededkey, res);
uj5u.com熱心網友回復:
使用 groovy find,它可能是這樣的,
def json = ...
def res = json.find { _,v ->
v.find { a ->
a.height == 2
}
}
println res
轉載請註明出處,本文鏈接:https://www.uj5u.com/gongcheng/324326.html
