有两个要比较的Json字符串。
1
"values": {
"-1487778947": {
"field1": "xxx",
"field2": "yyy",
"field3": {
"zzz": {
"field4": 21,
"field5": 28
}
}
},
"-1820451085": {
"field1": "fgf",
"field2": "dfd",
"field3": {
"zzz": {
"field4": 56,
"field5": 78
}
}
},
}2
"values": {
"343434-35454-232467498": { // ignore this value
"field1": "xxx", // compare these fields
"field2": "yyy", // compare these fields
"field3": { // compare these fields
"zzz": { // compare these fields
"field4": 21, // compare these fields
"field5": 28 // compare these fields
}
}
},
"486787-4546-787344353": { // ignore this value
"field1": "fgf", // compare these fields
"field2": "dfd", // compare these fields
"field3": { // compare these fields
"zzz": { // compare these fields
"field4": 56, // compare these fields
"field5": 78 // compare these fields
}
}
},
}我想忽略这些对象的键,只匹配内部字段。对于JsonAssert或任何其他库,这是可能的吗?我们可以使用自定义来忽略这些字段。但没有找到仅忽略对象键并验证子对象值的方法。
发布于 2021-11-17 08:42:41
使用Jayway-JSONPath,您只能从这两个JSON获取子节点,从而忽略键。
输入JSON
{
"values": {
"-1487778947": {
"field1": "xxx",
"field2": "yyy",
"field3": {
"zzz": {
"field4": 21,
"field5": 28
}
}
},
"-1820451085": {
"field1": "fgf",
"field2": "dfd",
"field3": {
"zzz": {
"field4": 56,
"field5": 78
}
}
}
}
}JSONPath
$.values.[*]输出
[
{
"field1" : "xxx",
"field2" : "yyy",
"field3" : {
"zzz" : {
"field4" : 21,
"field5" : 28
}
}
},
{
"field1" : "fgf",
"field2" : "dfd",
"field3" : {
"zzz" : {
"field4" : 56,
"field5" : 78
}
}
}
]在线测试工具:
https://stackoverflow.com/questions/69993422
复制相似问题