·您现在的位置: 云翼网络 >> 文章中心 >> 网站建设 >> 网站建设开发 >> ASP.NET网站开发 >> XML and JSON 验证

XML and JSON 验证

作者:佚名      ASP.NET网站开发编辑:admin      更新时间:2022-07-23

xml and JSON 验证

function ChkJson(strJson) { //判断Json格式是否正确 if (strJson == null || strJson == "") return true;

try { JSON.parse(strJson); return true; } catch (ex) { return false; }}function validateXML(strXml, msg) { debugger; msg = msg + "\r\r错误信息:\r"; // // code for IE // if (window.ActiveXObject) { // var xmlDoc = new ActiveXObject("Microsoft.XMLDOM"); // xmlDoc.async = "false"; // xmlDoc.loadXML(document.all(txt).value);

// if (xmlDoc.parseError.errorCode != 0) { // txt = "Error Code: " + xmlDoc.parseError.errorCode + "\n"; // txt = txt + "Error Reason: " + xmlDoc.parseError.reason; // txt = txt + "Error Line: " + xmlDoc.parseError.line; // alert(msg + txt); // return false; // } else { // //alert("没有发现错误"); // return true; // } // } // code for Mozilla, Firefox, Opera, Chrome, etc. // else if (document.implementation.createDocument) { var parser = new DOMParser(); //var text = txt; var xmlDoc = parser.parseFromString(strXml, "text/xml");

if (xmlDoc.documentElement.nodeName == "parsererror") { alert(msg + xmlDoc.documentElement.childNodes[0].nodeValue); return false; } else { //alert("没有发现错误"); return true; } } // // code for IE else if (window.ActiveXObject) ){ alert('请使用Chrome浏览器'); return false; } else { alert('您的浏览器无法处理XML验证'); return false; }}