How to compare two Json objects using C#

后端 未结 6 1813
我在风中等你
我在风中等你 2021-02-05 19:57

I have two Json objects as below need to be compared. I am using Newtonsoft libraries for Json parsing.

string InstanceExpected = jsonExpected;
string InstanceAc         


        
6条回答
  •  再見小時候
    2021-02-05 20:44

    After you deserialize the json to C# object the correct way is to implement the IComparable interface in the deserialized class and compare the 2 objects.

    So:

    using System;
    using System.Collections.Generic;
    
    class MyObj : IComparable
    {
        public string Name { get; set; }
        public string ObjectID { get; set; }
    
        public int CompareTo(MyObj other)
        {
            if ((this.Name.CompareTo(other.Name) == 0) &&
                (this.ObjectID.CompareTo(other.ObjectID) == 0))
            {
                return 0;
            }
            return -1;
        }
    }
    

提交回复
热议问题