且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

Groovy - 比较两个JSON对象(相同的结构)并返回包含差异的ArrayList

更新时间:2022-05-09 22:42:42

你正在比较 secondJSON.it it 这里只是一个字符串键,因为这个键没有值,你得到 null )。

You are comparing always against secondJSON.it (it here is just a String key, and since there is no value for this key, you get null).

你必须使用: p>

You will have to use:

secondJSON.get(it.key)
// or secondJSON."$it.key"

从其他地图访问该键。请注意,如果您有有效的 null 值,您可能需要检查 containsKey

to access the key from the other map. Be aware, that you might want to check with containsKey if there actually is an entry, if you have valid null values in the map.

在groovy中在地图上使用不存在的属性只是尝试在地图中查找该字符串键。

Using non-existing properties on a map in groovy will just try to look up that string key in the map.

assert [:].it==null // key `String it` does not exist
assert [:].SevenDrunkenNights==null // same here
assert [it:1].it==1 // now it does
assert [:].get('it')==null // same same for `get`
assert [it:1].get('it')==1