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

使用するクラス

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 に答える
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 Inherits EqualityComparer(Of testObjects) Public Overrides Function 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 に答える