gpt4 book ai didi

javascript - 如何删除特定的嵌套 json 属性

转载 作者:行者123 更新时间:2023-11-30 17:29:02 25 4
gpt4 key购买 nike

我想删除出现的 __metadata 。如何做到这一点我搜索了这个问题,但没有得到确切的解决方案。希望你能帮帮我。

{
"__metadata":{
"uri":"asd",
"type":"DP_CART_SRV.DPCARTHeader"
},
"Dealer":{
"__metadata":{
"uri":"asd"
},
"CustomerNo":"",
"Name1":"",
"Name2":""
},
"Retailer":{
"__metadata":{
"uri":"asd"
},
"CustomerNo":"",
"Name1":"",
"Name2":""
},
"Cart":"0000000081",
"Type":"SH",
"CreatedOn":"/Date(1399420800000)/",
"ChangedOn":null,
"OrderId":"",
"OrderType":"",
"ReqDeliveryDate":"2014/05/31",
"OrderValue":"11.00",
"DocCurrency":"EUR",
"NetValue":"11.00",
"Freight":"0.00",
"Discount":"0.00",
"Tax":"0.00",
"Remarks":"Remarks",
"Items":{
"results":[
{
"__metadata":{
"uri":"asd"
},
"Cart":"0000000081",
"ItemNo":"000010",
"ProductID":"FAN_FG1",
"ItemDesc":"Finshed product FAN",
"Quantity":"1.000",
"Uom":"KAR",
"Price":"11.00",
"Currency":"EUR",
"Available":"",
"DeleteStatus":""
},
{
"__metadata":{
"uri":"",
"type":""
},
"DeleteStatus":"",
"Available":"",
"Currency":"",
"Price":"",
"Uom":"",
"Quantity":"",
"ItemDesc":"",
"ProductID":"",
"ItemNo":"",
"Cart":""
}
]
}
}

最佳答案

这是一个有趣的。不是很明显的方法:

var result = JSON.parse(JSON.stringify(obj, function(key, value) {
return key !== '__metadata' ? value : undefined;
}));

它使用replacer 函数JSON.stringify方法接受:

replacer If a function, transforms values and properties encountered while stringifying; if an array, specifies the set of properties included in objects in the final string. A detailed description of the replacer function is provided in the javaScript guide article Using native JSON.

但是请注意,这不是 JSON.stringify 和 JSON.parse 的用途。当然 result 将不再是原始对象,而是 < em>新对象。

演示:http://jsfiddle.net/7Gj47/

关于javascript - 如何删除特定的嵌套 json 属性,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23580869/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com