如何检查两个JSON对象是否相等?
我试图发现两个JSON字符串是否相等。
这是我之前尝试过的
var obj1 = Json.Decode("{\"ValueA\":1,\"ValueB\":2}") var obj2 = Json.Decode("{\"ValueB\":2,\"ValueA\":1}") // But then there seems to be no way to compare the two objects?
当然,必须存在一种优雅简单的方式来实现我认为的常见任务?
另一种比较json的方法 – 将JSON与JToken.DeepEquals进行比较
JObject o1 = new JObject { { "Integer", 12345 }, { "String", "A string" }, { "Items", new JArray(1, 2) } }; JObject o2 = new JObject { { "Integer", 12345 }, { "String", "A string" }, { "Items", new JArray(1, 2) } }; Console.WriteLine(JToken.DeepEquals(o1, o2));
我可以使用下面的代码在一定程度上比较两个JSON。 对于原始课程,我能够在很大程度上得到结果。
我希望通过一些更多的帮助和调整,可以使下面的内容更加强大
static void Main(string[] args) { var o = new { ValueA = "",//Comparison Works ValueB = "",//Comparison Works ValueC = new { ValueD = "", ValueE = "" },//Comparison Works ValueF = new[] { new { ValueG = "", ValueH = "" } },//Fails if the array values are out of order ValueI = new SortedDictionary
您可以使用Compare .NET Objects lib来检查两个对象实例是否相等。 它知道如何比较列表,字典等,并深入比较整个对象图。 它还支持详细报告不同的内容,并具有您可能希望将来使用的更多function。