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

All*_*lly 9 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?
Run Code Online (Sandbox Code Playgroud)

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

RL8*_*L89 14

另一种比较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));
Run Code Online (Sandbox Code Playgroud)

  • 这是[Json.NET](http://www.newtonsoft.com/json/help/html/T_Newtonsoft_Json_Linq_JToken.htm)的东西,对吗?如果是这样,那么说.. (2认同)

San*_*esh 0

我能够使用下面的代码在某种程度上比较两个 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<object, object>()//Comparison works
        };

        var t = JsonConvert.DeserializeAnonymousType(
            "{\"ValueA\":1,\"ValueB\":2, \"ValueC\":{\"ValueE\":2,\"ValueD\":1}, \"ValueF\":[{\"ValueG\":10,\"ValueH\":25}],\"ValueI\":{\"Test1\":\"Val1\",\"Test2\":\"Val1\"}}", o);

        var q = JsonConvert.DeserializeAnonymousType(
            "{\"ValueB\":2,\"ValueA\":1, \"ValueC\":{\"ValueD\":1,\"ValueE\":2}, \"ValueF\":[{\"ValueH\":25,\"ValueG\":10}],\"ValueI\":{\"Test2\":\"Val1\",\"Test1\":\"Val1\"}}", o);

        var prop = t.GetType().GetProperties();

        var match = true;

        foreach (var item in prop)
        {
            var type = item.PropertyType;

            if (type.IsArray)
            {
                var v1 = item.GetValue(t) as Array;
                var v2 = item.GetValue(q) as Array;
                if ((v1 != null && v2 != null))
                {
                    if ((v1.Length != v2.Length))
                    {
                        match = false;
                        break;
                    }
                    for (int i = 0; i < v1.Length; i++)
                    {
                        if (!v1.GetValue(i).Equals(v2.GetValue(i)))
                        {
                            match = false;
                            break;
                        }
                    }
                }
                else if ((v1 == null && v2 != null) || (v1 != null && v2 == null))
                {
                    match = false;
                    break;
                }
            }
            else if (type.Name.Contains("Dictionary"))
            {
                var v1 = (SortedDictionary<object, object>)item.GetValue(t);
                var v2 = item.GetValue(q) as SortedDictionary<object, object>;
                foreach (var ar in v1)
                {
                    if (!v2.Contains(ar))
                    {
                        match = false;
                        break;
                    }
                }
            }
            else if (!item.GetValue(t).Equals(item.GetValue(q)))
            {
                var v1 = item.GetValue(t);
                var v2 = item.GetValue(q);
                match = v1.ToString().Equals(v2.ToString());
                match = false;
                break;
            }
        }

        if (!match)
        {
            Console.WriteLine("Objects do not match");
        }
    }
Run Code Online (Sandbox Code Playgroud)