开发者

Compare two JObjects or JArray

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


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
}
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜