Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Compare two JObjects or JArray

Tags:

I have this WPF application which gets data from REST web service and returns a JSON data. Then this data will be converted to xml. This xml file later will be converted back to JSON to be compared with new JSON data from REST web service calling same function. How do I do this?

Here is a sample of what I did:

HTTPGet req = new HTTPGet();             req.Request("http://restservice//function");             string str= req.ResponseBody;             StringBuilder xmlTemplate = new StringBuilder("{\"?xml\":{\"@version\": \"1.0\",\"@standalone\": \"no\"},\"root\":REPLACE }");             StringBuilder json = xmlTemplate.Replace(Constants.Constants.XMLREPLACEVAL, str); //this so that it will be same with the JObject from XML file             JObject jObject1 = JObject.Parse(json.ToString());              XmlDocument doc = new XmlDocument();             string xml = File.ReadAllText("json.xml");             doc.LoadXml(xml);             string jsonText = JsonConvert.SerializeXmlNode(doc);             JObject jObject2 = JObject.Parse(jsonText);              if(jObject1.Equals(jObject2))                 //DO SOMETHING 
like image 835
patlimosnero Avatar asked Jul 25 '11 10:07

patlimosnero


1 Answers

It seems that JObject doesn't override Equals method. Nevertheless, JObject inherits JToken class and JToken has static method DeepEquals, which can be used to determine if one JToken is equal to other JToken. So, you can do something like this:

if (JToken.DeepEquals(jObject1, jObject2)) {     //DO SOMETHING } 
like image 112
Pavel Surmenok Avatar answered Sep 19 '22 09:09

Pavel Surmenok