- 从服务器接收的json对象包含循环引用。在使用对象之前,您必须首先
$ref
从对象中删除所有属性,这意味着`$ref - “1”`必须放置此链接指向的对象来代替您。
在您的情况下,它可能指向id为1的User对象
为此,您应该在github上查看DouglasCrockfords插件。有一个cycle.js可以为您完成工作。
或者您可以使用以下代码(未经测试):
function resolveReferences(json) { if (typeof json === 'string') json = JSON.parse(json); var byid = {}, // all objects by id refs = []; // references to objects that could not be resolved json = (function recurse(obj, prop, parent) { if (typeof obj !== 'object' || !obj) // a primitive value return obj; if ("$ref" in obj) { // a reference var ref = obj.$ref; if (ref in byid) return byid[ref]; // else we have to make it lazy: refs.push([parent, prop, ref]); return; } else if ("$id" in obj) { var id = obj.$id; delete obj.$id; if ("$values" in obj) // an array obj = obj.$values.map(recurse); else // a plain object for (var prop in obj) obj[prop] = recurse(obj[prop], prop, obj) byid[id] = obj; } return obj; })(json); // run it! for (var i=0; i<refs.length; i++) { // resolve previously unknown references var ref = refs[i]; ref[0][ref[1]] = byid[refs[2]]; // Notice that this throws if you put in a reference at top-level } return json;}让我知道是否有帮助!



