我有以下 JSON 响应
{
"r": [{
"IsDefault": false,
"re": {
"Name": "Depo"
},
"Valid": "Oct8, 2013",
"Clg":[ {
"Name": "james",
"Rate": 0.05
},
{
"Name": "Jack",
"Rate": 0.55
},
{
"Name": "Mcd",
"Rate": 0.01,
}],
},
{
"IsDefault": false,
"re": {
"Name": "Depo"
},
"Valid": "Oct8, 2013",
"Clg": [{
"Name": "james",
"Rate": 0.05
},
{
"Name": "Jack",
"Rate": 0.55
},
{
"Name": "Mcd",
"Rate": 0.01,
}],
},
{
"IsDefault": false,
"re": {
"Name": "Depo"
},
"Valid": "Oct8, 2013",
"Clg": [{
"Name": "james",
"Rate": 0.05
},
{
"Name": "Jack",
"Rate": 0.55
},
{
"Name": "Mcd",
"Rate": 0.01,
}],
}]
}
我正在使用 $.getJSON 方法加载 JSON 数据,并通过检查它是否为数组将其添加到集合中,如下所示。它按预期工作正常。
var res = loadFromJson(result.r);
loadFromJson = function(input) {
if (_.isArray(input)) {
console.log("Inside Array");
var collection = new CompeCollection();
_.each(input, function(modData) {
collection.add(loadFromJson(modData));
console.log("collection length:"+modData.length);
});
return collection;
}
};
但是,当我尝试指定我的 JSON 响应的确切路径(r.Clg)时,即使它是有效响应,它甚至没有达到上述条件。
var res = loadFromJson(result.r.Clg);
loadFromJson = function(input) {
if (_.isArray(input)) {
console.log("Inside Array");
var collection = new CompeCollection();
_.each(input, function(modData) {
collection.add(loadFromJson(modData));
console.log("collection length:"+modData.length);
});
return collection;
}
};
当我需要指定实际路径或完整路径时应该怎么做?
当我传递更具体的 json 值时,谁能告诉我可能是什么问题?
提前致谢。