When I get an array of objects in JSON response, they might be represent in a different order.
Sometimes I get this
JSON.parse(response.body) # => [{"a" => "b"}, {"c" => "d"}]
or this
JSON.parse(response.body) # => [{"c" => "d"}, {"a" => "b"}]
But for me both results are correct. What is the easiest way to test which objects I have in response regardless of their order?
You can convert it into a set-like structure using a hash.
first_response.inject({}){|s, h| s[h] = true; s}
will be the same as
second_response.inject({}){|s, h| s[h] = true; s}
连同其其他有用的功能, json规范 gem还具有be_json_eql
匹配器,该匹配器的工作无需考虑顺序。
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.