我需要迭代一個串列,該串列可以具有相同的標簽名稱但不同的索引,如 0,1,2。所以我需要迭代串列,取出公共索引,然后從該索引中取出名稱和值標簽并形成另一個串列。
要求:
{
"characteristic": [
{
"name": "BucketName",
"value": "testName0",
"@type": "bucketInfo",
"arrayIndex": "0"
},
{
"name": "BucketName",
"value": "testName1",
"@type": "bucketInfo",
"arrayIndex": "1"
},
{
"name": "BucketName",
"value": "testName2",
"@type": "bucketInfo",
"arrayIndex": "2"
},
{
"name": "BucketId",
"value": "testId0",
"@type": "bucketInfo",
"arrayIndex": "0"
},
{
"name": "BucketId",
"value": "testId1",
"@type": "bucketInfo",
"arrayIndex": "1"
},
{
"name": "BucketId",
"value": "testId2",
"@type": "bucketInfo",
"arrayIndex": "2"
}
]
}
需要回復:
{
"bucketList": [
{
"BucketName": "testName0",
"BucketId": "testId0"
},
{
"BucketName": "testName1",
"BucketId": "testId1"
},
{
"BucketName": "testName2",
"BucketId": "testId2"
}
]
}
我們如何根據 alist 的索引實作這一點?
當有更多元素時,如何處理這種情況以跳過值未出現并僅添加即將出現的標簽。示例請求:
{
"characteristic": [
{
"name": "BucketName",
"value": "testName0",
"@type": "bucketInfo",
"arrayIndex": "0"
},
{
"name": "BucketId",
"value": "testId0",
"@type": "bucketInfo",
"arrayIndex": "0"
},
{
"name": "BucketType",
"value": "testType1",
"@type": "bucketInfo",
"arrayIndex": "1"
},
{
"name": "BucketId",
"value": "testId1",
"@type": "bucketInfo",
"arrayIndex": "1"
},
{
"name": "BucketName",
"value": "testName2",
"@type": "bucketInfo",
"arrayIndex": "2"
},
{
"name": "BucketId",
"value": "testId2",
"@type": "bucketInfo",
"arrayIndex": "2"
},
{
"name": "BucketId",
"value": "testId3",
"@type": "bucketInfo",
"arrayIndex": "3"
},
{
"name": "BucketName",
"value": "testName3",
"@type": "bucketInfo",
"arrayIndex": "3"
},
{
"name": "BucketData",
"value": "testData3",
"@type": "bucketInfo",
"arrayIndex": "3"
}
]
}
預期回應:
{
"bucketlist": [
{
"BucketName": "testName0",
"BucketId": "testId0"
},
{
"BucketType": "testType1",
"BucketId": "testId1"
},
{
"BucketName": "testName2",
"BucketId": "testId2"
},
{
"BucketName": "testName3",
"BucketId": "testId3",
"BucketData": "testData3"
}
]
}
uj5u.com熱心網友回復:
您可以應用兩個連續的移位變換。以“@(2,arrayIndex)”為公因子,將第一個變換中常用標記陣列下的元素組合起來,然后在第二個變換中隨意顯示,如
[
{
"operation": "shift",
"spec": {
"characteristic": {
"*": {
"value": {
"@(1,value)": "@(2,arrayIndex).@(2,name)"
}
}
}
}
},
{
"operation": "shift",
"spec": {
"*": "bucketList[]"
}
}
]

轉載請註明出處,本文鏈接:https://www.uj5u.com/gongcheng/388868.html
