在 http://www.ecjson.com/jsondiff 看到1款js的json比对插件,觉得挺好就拔了下来,仅供大家学习使用
优点: json格式检测,高亮显示,json比对
3种显示模式
默认模式
<script> $(function () { var JSON_PARAM = utils._getURLParameter('json'); // this needs to be in a composite var jsonComposite=new JSONComposite({ el : $('#json-composite-placeholder'), json : JSON_PARAM }); jsonComposite.primaryValidator.json='{"name":"11111","email": "chy2z@163.com","age":}'; jsonComposite.primaryValidator._checkForJSON(); }); </script>
拆分模式
<script>
$(function () {
var JSON_PARAM = utils._getURLParameter('json');
// this needs to be in a composite
var jsonComposite=new JSONComposite({
el : $('#json-composite-placeholder'),
json : JSON_PARAM
});
jsonComposite.primaryValidator._appendResult('{"name":"11111"}');
jsonComposite.secondaryValidator._appendResult('{"name":"22222"}');
jsonComposite.enterSplitMode();
});
</script>
比对模式
<script>
$(function () {
var JSON_PARAM = utils._getURLParameter('json');
// this needs to be in a composite
var jsonComposite=new JSONComposite({
el : $('#json-composite-placeholder'),
json : JSON_PARAM
});
jsonComposite.primaryValidator._appendResult('{"name":"11111","age":21}');
jsonComposite.secondaryValidator._appendResult('{"name":"22222"}');
jsonComposite.enterSplitMode();
//进入diff模式前,需要SplitMode验证json格式
jsonComposite.enterDiffMode();
});
</script>
代码下载: