#以下是我的JSON
{
"121874": {
"sku": 121874,
"reference": "002331",
"reference2": "002331",
"description": "USB-C - Lightning Cable",
"warehouse_id": 1,
"weight": 70,
"dimensions": {
"length": 92,
"width": 87,
"height": 20
},
"items_in_box": 1,
"level": "40",
"atp": "40",
"master_level": "40",
"master_atp": "40",
"status": 1,
"status_string": "on",
"lot_number": "",
"tht_alert": "none",
"tht_date": false,
"tht_minimum": false,
"last_update": {
"date": "2022-04-04 08:54:41",
"timezone_type": 3,
"timezone": "Europe/Brussels"
},
"last_atp_update": {
"date": "2022-04-04 08:54:41",
"timezone_type": 3,
"timezone": "Europe/Brussels"
}
},
"121995": {
"sku": 121995,
"reference": "002234SL",
"reference2": "002234SL",
"description": "AirPods 3 (Silver)",
"warehouse_id": 1,
"weight": 0,
"dimensions": {
"length": 0,
"width": 0,
"height": 0
},
"items_in_box": 1,
"level": 0,
"atp": 0,
"master_level": 0,
"master_atp": 0,
"status": 1,
"status_string": "on",
"lot_number": "",
"tht_alert": "none",
"tht_date": false,
"tht_minimum": false,
"last_update": {
"date": "2022-04-01 03:26:59",
"timezone_type": 3,
"timezone": "Europe/Brussels"
},
"last_atp_update": {
"date": "2022-04-01 03:26:59",
"timezone_type": 3,
"timezone": "Europe/Brussels"}
#我有reference 這個數據. 想對應 sku 。 謝謝你們幫助!
你题目的解答代码如下:
obj={ "121874": { "sku": 121874, "reference": "002331", "reference2": "002331", "description": "USB-C - Lightning Cable", "warehouse_id": 1, "weight": 70, "dimensions": { "length": 92, "width": 87, "height": 20 }, "items_in_box": 1, "level": "40", "atp":
"40", "master_level": "40", "master_atp": "40", "status": 1, "status_string": "on", "lot_number": "", "tht_alert": "none", "tht_date": false, "tht_minimum": false, "last_update": { "date": "2022-04-04 08:54:41", "timezone_type": 3, "timezone": "Europe/Brussels"
}, "last_atp_update": { "date": "2022-04-04 08:54:41", "timezone_type": 3, "timezone": "Europe/Brussels" } }, "121995": { "sku": 121995, "reference": "002234SL", "reference2": "002234SL", "description": "AirPods 3 (Silver)", "warehouse_id": 1, "weight":
0, "dimensions": { "length": 0, "width": 0, "height": 0 }, "items_in_box": 1, "level": 0, "atp": 0, "master_level": 0, "master_atp": 0, "status": 1, "status_string": "on", "lot_number": "", "tht_alert": "none", "tht_date": false, "tht_minimum": false,
"last_update": { "date": "2022-04-01 03:26:59", "timezone_type": 3, "timezone": "Europe/Brussels" }, "last_atp_update": { "date": "2022-04-01 03:26:59", "timezone_type": 3, "timezone": "Europe/Brussels"}
reference = "002234SL";
sku = "";
for (n in obj) {
if (obj[n].reference==reference){
sku = obj[n].sku;
break;
}
}
如有帮助,请点击我的回答下方的【采纳该答案】按钮帮忙采纳下,谢谢!
转Java实体操作方便些。
Json在线转换,可以给你转成对应的实体。
下载一个fastjson包,把字符串转换为json对象,然后获取数据。
您好,我是有问必答小助手,您的问题已经有小伙伴帮您解答,感谢您对有问必答的支持与关注!const listing = pm.variables.get('listing');
pm.variables.set('listing', listing);
console.log(listing)
const listings = pm.response.json()
console.log(listings)
obj= listings;
const reference = listing;
for (n in obj) {
if (obj[n].reference==reference){
sku = obj[n].sku;
break;
}}
console.log(sku)
#但如果 const reference = listing; 是個 loop array . 好似不行了