0

I have a dynamic JSON which I am deserializing using Newronsoft JSON.

    if (string.IsNullOrEmpty(JsonString) == false)
    {
        DeserializedJson = JsonConvert.DeserializeObject<dynamic>(JsonString);
    }

The Json is an array and one of the node is results. I have 2 lists node1 and node2. 2 exact same Json stirngs coming from different sources are populating these lists with items from results node.

public List<object> node1;
public List<object> node2
int Node1TotalResults = Enumerable.Count(Searchers[0].DeserializedJson.Results);
int Node2TotalResults = Enumerable.Count(Searchers[1].DeserializedJson.Results);
for (int i = 0; i <= (Node1TotalResults - 1); i++)
{
    node1.Add(Searchers[0].DeserializedJson.Results[i]
}
//Same stuff for node2

Now to Compare, I tried 2 different approaches and none of them see to work. But if I print them manually, both lists look similar. Here is how I compared them:

//1st Method:
  if (node1.SequenceEqual(node2))
  {
      result = true;
  }

//2nd Method:
  if(object.Equals(node1, node2))
  {
            result = true;
  }

Both of these methods return false. Any thing here that I can do?

Here is the Sample JSON:
 "Results": [
    {
        "MlsCode": "NON-MLS",
        "MlsAgentId": "45D7D24E253F46A88458B88891E05A1D",
        "UserId": null,
        "FirstName": "Eric",
        "LastName": "Heal",
        "OfficeName": "Nick Salamone Real Estate",
        "MlsOfficeId": "62B8C79E0E0E4D63A7EEF2313BAC98DF",
        "Phones": [
            "3125688028"
        ]
     }
]

I should add, when I convert this json into c# classes and add individual key/item from results array to these lists and compare them using the first method above, it works fine.

Atit
  • 15
  • 1
  • 7

2 Answers2

1

Use JToken.DeepEquals to compare the two JArrays

string jsonString1 = ...;
string jsonString2 = ...;

var obj1 = JObject.Parse(jsonString1);
var obj2 = JObject.Parse(jsonString2);

var areEqual = JToken.DeepEquals(obj1["Results"], obj2["Results"]);
dcastro
  • 66,540
  • 21
  • 145
  • 155
0

Json :

{
    "Results": [
        {
            "MlsCode": "NON-MLS",
            "MlsAgentId": "45D7D24E253F46A88458B88891E05A1D",
            "UserId": null,
            "FirstName": "Eric",
            "LastName": "Heal",
            "OfficeName": "NickSalamoneRealEstate",
            "MlsOfficeId": "62B8C79E0E0E4D63A7EEF2313BAC98DF",
            "Phones": [
                "3125688028"
            ]
        }
    ]
}

Would results in (Copy + Paste Special > Paste JSON as classes ):

    public class Rootobject
    {
        public Result[] Results { get; set; }
    }

    public class Result
    {
        public string MlsCode { get; set; }
        public string MlsAgentId { get; set; }
        public object UserId { get; set; }
        public string FirstName { get; set; }
        public string LastName { get; set; }
        public string OfficeName { get; set; }
        public string MlsOfficeId { get; set; }
        public string[] Phones { get; set; }
    }

To deserialize :

public static class Extensions
{
    public  static bool DeserializeJson<T>(this String str, out T item)
    {
        item = default(T);
        try
        {
            item = new JavaScriptSerializer().Deserialize<T>(str);
            return true;
        }
        catch (Exception ex)
        {
            return false;
        }
    }
}

After that you could write:

var jsonStr = "{\"Results\": [...]}";
Rootobject json;
if(jsonStr.DeserializeJson(out json)){
    //do something with json
}

To answer question. Easiest would be to check 2 objects serialized versions.

Margus
  • 19,694
  • 14
  • 55
  • 103
  • 1
    Your post explains how to deserialize a JSON string into a structured object - that's not what the OP asked at all. – dcastro Jun 03 '15 at 14:46