如何检查两个JSON对象是否相等?

时间:2014-02-04 14:53:23

标签: c# 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?

当然,必须存在一种优雅简单的方式来实现我认为的常见任务?

1 个答案:

答案 0 :(得分:12)

比较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));
相关问题