我正在使用 JSON 文件并在 Swagger 2.0 Parser and validator 上对其进行了验证
它验证它但给出循环引用错误,是否有任何免费工具或网站来检测文件中循环引用的位置。
最佳答案
我想你要找的已经得到了回答 here .
只需打开您的浏览器控制台并输入以下 javascript :
function isCyclic(obj) {
var keys = [];
var stack = [];
var stackSet = new Set();
var detected = false;
function detect(obj, key) {
if (typeof obj != 'object') { return; }
if (stackSet.has(obj)) { // it's cyclic! Print the object and its locations.
var oldindex = stack.indexOf(obj);
var l1 = keys.join('.') + '.' + key;
var l2 = keys.slice(0, oldindex + 1).join('.');
console.log('CIRCULAR: ' + l1 + ' = ' + l2 + ' = ' + obj);
console.log(obj);
detected = true;
return;
}
keys.push(key);
stack.push(obj);
stackSet.add(obj);
for (var k in obj) { //dive on the object's children
if (obj.hasOwnProperty(k)) { detect(obj[k], k); }
}
keys.pop();
stack.pop();
stackSet.delete(obj);
return;
}
detect(obj, 'obj');
return detected;
}
然后你打电话
IsCyclic(/*Json String*/)
,结果将显示循环引用的位置。
关于circular-dependency - 在 JSON 模式中检查循环依赖的任何工具,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43199331/