【问题标题】:How to compare 2 JSON structures using VB.NET?如何使用 VB.NET 比较 2 个 JSON 结构?
【发布时间】:2012-12-14 07:36:28
【问题描述】:

我是 JSON.NET 的新手。我正在尝试使用 VB.NET 比较 2 个 JSON 结构,例如

{ "attrs":[ { "name":"_DB_ATTR_OSD_PARENT_", "column":"OsDeployerParent", "type":"Integer", "enumName":null }, { "name":"_DB_ATTR_SMALLICON_", "column":"CurrentSmallIcon", "type":"Enum" } ] }

请有人帮助我。

谢谢。

【问题讨论】:

    标签: vb.net json compare


    【解决方案1】:

    要使用的类

    public class testObjects
    {
        public List<testObject> attrs;
    }
    public class testObject
    {
        public string name;
        public string column;
        public string type;
        public string enumName;
    
    }
    

    备课比较

    class ObjectComparer : EqualityComparer<testObject>
    {
        public override bool Equals(testObject c1, testObject c2)
        {
            if (c1.name == c2.name &&
                c1.column == c2.column &&
                c1.enumName == c2.enumName &&
                c1.type == c2.type)
            {
                return true;
            }
            else
            {
                return false;
            }
        }
    
        public override int GetHashCode(testObject c)
        {
            int hash = 23;
            if (c.name != null) hash = hash * 37 + c.name.GetHashCode();
            if (c.column != null) hash = hash * 37 + c.column.GetHashCode();
            if (c.enumName != null) hash = hash * 37 + c.enumName.GetHashCode();
            if (c.type != null) hash = hash * 37 + c.type.GetHashCode();
            return hash;
        }
    }
    

    操作从这里开始

    string jsonObjects = @"{ 'attrs':[ { 'name':'_DB_ATTR_OSD_PARENT_', 'column':'OsDeployerParent', 'type':'Integer', 'enumName':null }, { 'name':'_DB_ATTR_SMALLICON_', 'column':'CurrentSmallIcon', 'type':'Enum' } ] }";
            System.Web.Script.Serialization.JavaScriptSerializer js = new System.Web.Script.Serialization.JavaScriptSerializer();
            var objects = js.Deserialize(jsonObjects, typeof(testObjects));
    
            ObjectComparer cmp = new ObjectComparer();
    
            testObjects deserializedObject = ((testObjects)objects);
    
            // this allows you to remove double entries
            var distinctObjects = deserializedObject.attrs.Distinct(cmp).ToList();
            Console.WriteLine(string.Format("duplicate objects count : {0}", (deserializedObject.attrs.Count - distinctObjects.Count).ToString()));
    
            //If you want to compare if the following code can be used individually
            testObject obj1 = ((testObjects)objects).attrs.First();
            testObject obj2 = ((testObjects)objects).attrs.Skip(1).First();
            bool isObjectEquals = cmp.Equals(obj1, obj2);
    
            Console.WriteLine(string.Format("Objects are {0}", isObjectEquals ? "Equals" : "Not Equals"));
    

    【讨论】:

      【解决方案2】:

      这是vb代码

      要使用的类

      Public Class testObjects
          Public attrs As List(Of testObject)
      End Class
      Public Class testObject
          Public name As String
          Public column As String
          Public type As String
          Public enumName As String
      
      End Class
      

      备课比较

      Class ObjectComparer
          Inherits EqualityComparer(Of testObject)
          Public Overrides Function Equals(c1 As testObject, c2 As testObject) As Boolean
              If c1.name = c2.name AndAlso c1.column = c2.column AndAlso c1.enumName = c2.enumName AndAlso c1.type = c2.type Then
                  Return True
              Else
                  Return False
              End If
          End Function
      
          Public Overrides Function GetHashCode(c As testObject) As Integer
              Dim hash As Integer = 23
              If c.name IsNot Nothing Then
                  hash = hash * 37 + c.name.GetHashCode()
              End If
              If c.column IsNot Nothing Then
                  hash = hash * 37 + c.column.GetHashCode()
              End If
              If c.enumName IsNot Nothing Then
                  hash = hash * 37 + c.enumName.GetHashCode()
              End If
              If c.type IsNot Nothing Then
                  hash = hash * 37 + c.type.GetHashCode()
              End If
              Return hash
          End Function
      End Class
      

      操作从这里开始

      Dim jsonObjects As String = "{ 'attrs':[ { 'name':'_DB_ATTR_OSD_PARENT_', 'column':'OsDeployerParent', 'type':'Integer', 'enumName':null }, { 'name':'_DB_ATTR_SMALLICON_', 'column':'CurrentSmallIcon', 'type':'Enum' } ] }"
      Dim js As New System.Web.Script.Serialization.JavaScriptSerializer()
      Dim objects = js.Deserialize(jsonObjects, GetType(testObjects))
      
      Dim cmp As New ObjectComparer()
      
      Dim deserializedObject As testObjects = DirectCast(objects, testObjects)
      
      ' this allows you to remove double entries
      Dim distinctObjects = deserializedObject.attrs.Distinct(cmp).ToList()
      Console.WriteLine(String.Format("duplicate objects count : {0}", (deserializedObject.attrs.Count - distinctObjects.Count).ToString()))
      
      'If you want to compare if the following code can be used individually
      Dim obj1 As testObject = DirectCast(objects, testObjects).attrs.First()
      Dim obj2 As testObject = DirectCast(objects, testObjects).attrs.Skip(1).First()
      Dim isObjectEquals As Boolean = cmp.Equals(obj1, obj2)
      
      Console.WriteLine(String.Format("Objects are {0}", If(isObjectEquals, "Equals", "Not Equals")))
      

      【讨论】:

      • 感谢您的回复!我尝试了此代码,但在运行它时出现此错误“算术运算导致溢出”。我需要比较上面示例中提到的 2 个 JSON 结构:比如比较 Dim jsonObjects1 As String = "..." 和 Dim jsonObjects2 As String = "..."
      • GetHashCode 返回一个对象的值,我们可以使用它来比较 2 个对象,但是如果我想要找到确切的变化怎么办。就像我有 2 个 JSON,比如Dim jsonObjects1 As String="{'attrs':[{'name':'_DB_ATTR_OSD_PARENT_','column':'OsDeployerParent', 'type':'Integer','enumName':null },{ 'name':'_DB_ATTR_SMALLICON_', 'column':'CurrentSmallIcon','type':'Enum'}]} "Dim jsonObjects2 As String={'attrs'[{'name':'_DB_ATTR_OSD_PARENT_','column':'OsDeployerParent', 'type':'Boo','enumName':null},{'name':'_DB_ATTR_SMALLICON_', 'column':'CurrentSmallIcon', 'type':'Enum'}]}" 它应该返回 'type':'Boo'
      【解决方案3】:

      新代码

      string jsonObjects = @"{ 'attrs':[ { 'name':'_DB_ATTR_OSD_PARENT_', 'column':'OsDeployerParent', 'type':'Integer', 'enumName':null }, { 'name':'_DB_ATTR_SMALLICON_', 'column':'CurrentSmallIcon', 'type':'Enum' } ] }";
          string jsonObjects2 = @"{ 'attrs':[ { 'name':'_DB_ATTR_OSD_PARENT_', 'column':'OsDeployerParent1', 'type':'Integer', 'enumName':null }, { 'name':'_DB_ATTR_SMALLICON_', 'column':'CurrentSmallIcon', 'type':'Enum' } ] }";
          System.Web.Script.Serialization.JavaScriptSerializer js = new System.Web.Script.Serialization.JavaScriptSerializer();
          var objects = js.Deserialize(jsonObjects, typeof(testObjects));
          var objects2 = js.Deserialize(jsonObjects2, typeof(testObjects));
      
          ObjectComparer2 cmp = new ObjectComparer2();
      
          testObjects deserializedObject = ((testObjects)objects);
          testObjects deserializedObject2 = ((testObjects)objects2);
          bool isObjectEquals = cmp.Equals(deserializedObject, deserializedObject2);
          Console.WriteLine(string.Format("Objects are {0}", isObjectEquals ? "Equals" : "Not Equals"));
      
      Class ObjectComparer
      Inherits EqualityComparer(Of testObject)
      Public Overrides Function Equals(c1 As testObject, c2 As testObject) As Boolean
          If c1.name = c2.name AndAlso c1.column = c2.column AndAlso c1.enumName = c2.enumName AndAlso c1.type = c2.type Then
              Return True
          Else
              Return False
          End If
      End Function
      
      Public Overrides Function GetHashCode(c As testObject) As Integer
          Dim hash As Integer = 23
          If c.name IsNot Nothing Then
              hash = hash * 37 + c.name.GetHashCode()
          End If
          If c.column IsNot Nothing Then
              hash = hash * 37 + c.column.GetHashCode()
          End If
          If c.enumName IsNot Nothing Then
              hash = hash * 37 + c.enumName.GetHashCode()
          End If
          If c.type IsNot Nothing Then
              hash = hash * 37 + c.type.GetHashCode()
          End If
          Return hash
      End Function
      

      结束类

      类 ObjectComparer2 继承 EqualityComparer(Of testObjects) 公共覆盖函数 Equals(c1 As testObjects, c2 As testObjects) As Boolean 将 cmp 调暗为新的 ObjectComparer()

          For Each obj1 As testObject In c1.attrs
              If Not c2.attrs.Any(Function(x) cmp.Equals(x, obj1)) Then
                  Return False
              End If
          Next
          Return True
      End Function
      
      Public Overrides Function GetHashCode(c As testObjects) As Integer
          Dim hash As Integer = 23
          For Each obj As testObject In c.attrs
              hash += obj.GetHashCode()
          Next
          Return hash
      End Function
      

      结束类

      【讨论】:

        猜你喜欢
        • 2013-05-25
        • 2016-01-15
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2020-11-14
        • 2019-10-31
        • 1970-01-01
        相关资源
        最近更新 更多