在空手道中使用 JavaScript 删除 JSON 对象属性
Delete JSON object properties with JavaScript in Karate
如何动态删除 JSON 对象属性?我试过这个:
* def delKey =
"""
function(json, key) {
delete json[key];
return json;
}
"""
* def aJson = { row: null, age: 35 , city: 'na'}
* print "Before: "
* print aJson
* def called = delKey(aJson, 'age')
* print "After: "
* print aJson
结果:
我想你还没有看到 remove
和 set
关键字。这是正确的方法。另请注意 print
语句如何支持逗号分隔的样式,以便打印得漂亮:
* def aJson = { row: null, age: 35 , city: 'na' }
* print 'Before: ', aJson
* remove aJson.age
* print 'After: ', aJson
如果需要动态删除key,可以使用JSAPIkarate.remove(name, path)
。参考文档 !
如何动态删除 JSON 对象属性?我试过这个:
* def delKey =
"""
function(json, key) {
delete json[key];
return json;
}
"""
* def aJson = { row: null, age: 35 , city: 'na'}
* print "Before: "
* print aJson
* def called = delKey(aJson, 'age')
* print "After: "
* print aJson
结果:
我想你还没有看到 remove
和 set
关键字。这是正确的方法。另请注意 print
语句如何支持逗号分隔的样式,以便打印得漂亮:
* def aJson = { row: null, age: 35 , city: 'na' }
* print 'Before: ', aJson
* remove aJson.age
* print 'After: ', aJson
如果需要动态删除key,可以使用JSAPIkarate.remove(name, path)
。参考文档 !