我正在使用Rails3&使用act_as_tree。如何在嵌套的json上迭代,以便找出两个节点之间的差异?
我想要比较的json如下所示:
{
"text": "To Do",
"children": [{
"text": "Go jogging",
"children":[]
}, {
"text": "Take a nap",
"children":[]
}]
}和
{
"text": "To Do",
"children": [{
"text": "Climb Everest",
"children":[]
}]
}发布于 2012-01-05 22:12:34
显示不同之处-我猜你需要将JSON反序列化为结构,并比较对象。在我看来,这是最简单的方法。
发布于 2014-10-07 06:05:13
请看一下https://rubygems.org/gems/json-compare
可以做一些事情来获得差异:
require 'yajl'
require 'json-compare'
json1 = File.new('spec/fixtures/twitter-search.json', 'r')
json2 = File.new('spec/fixtures/twitter-search2.json', 'r')
old, new = Yajl::Parser.parse(json1), Yajl::Parser.parse(json2)
result = JsonCompare.get_diff(old, new)发布于 2014-11-22 23:24:05
正如OhadR提到的,反序列化和比较对象似乎是一种可行的方法。
要做到这一点,请查看以下gem:
https://github.com/liufengyun/hashdiff
它甚至有一个比较JSON和YAML结构并显示差异的online demo。
https://stackoverflow.com/questions/8739993
复制相似问题