I'm trying to discover if two JSON strings are equal.
This is what I previously tried
var obj1 = Json.Decode("{\"ValueA\":1,\"ValueB\":2}")
var obj2 = Json.Decode("{\"ValueB\":2,\"ValueA\":1}")
// But then there seems to be no way to compare the two objects?
Surely there must exist an elegant simple way to what I thought would be a common task?
You could use the Compare .NET Objects lib to check if the two object instances are equal. It knows how to compare lists, dictionaries, etc. and deep compares the whole object graph. It also supports detailed reporting of what is different and has many more features you might want to use in the future.
Another way to compare json - Comparing JSON with JToken.DeepEquals
JObject o1 = new JObject
{
{ "Integer", 12345 },
{ "String", "A string" },
{ "Items", new JArray(1, 2) }
};
JObject o2 = new JObject
{
{ "Integer", 12345 },
{ "String", "A string" },
{ "Items", new JArray(1, 2) }
};
Console.WriteLine(JToken.DeepEquals(o1, o2));
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With