0

我是 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" } ] }

请有人可以帮助我。

谢谢。

4

3 回答 3

0

新代码

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 Dim cmp As New 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

结束类

于 2012-12-17T20:56:28.810 回答
0

这是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")))
于 2012-12-14T13:59:11.130 回答
0

要使用的类

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"));
于 2012-12-14T13:54:19.170 回答