Compare two JObjects or JArray

♀尐吖头ヾ 提交于 2019-12-08 15:49:54

问题


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

回答1:


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
}


来源:https://stackoverflow.com/questions/6815006/compare-two-jobjects-or-jarray

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!