我无法从JSON对象解析属性。由于这只是一个JSON对象,我不确定是否需要执行$.each
或者我可以直接获取属性data.d.property
。有人可以解释我如何从这个对象中获取属性值吗?
这就是我所拥有的:
jQ(document).ready(function() {
var listDataURL = "";
var root = getParameterByName('RootFolder');
var rootFolderID = getParameterByName("ID");
var docSetTitle = root.substr(root.lastIndexOf('/') + 1);
listDataURL = "http://SomeSite/sites/TestWF/_vti_bin/listdata.svc/ResumeBank(" + rootFolderID +")";
//use the getJSON mehtod of Jquery
jQ.getJSON(listDataURL, function(data, status, jqXHR) {
console.log(jqXHR.responseText);
//iterate through all the items
jQ.each(data.d.results, function(i, result) {
//get child items count for the current set
ChildItemCount = result.ItemChildCount;
NumOfItemsApproved = result.ApprovalCounter;
});
if (ChildItemCount !== NumOfItemsApproved){
alert("Ah ah! The ApprovalCounter got skewed and needs to be fixed.");
}
if (ChildItemCount === NumOfItemsApproved){
alert("ApprovalCounter looks good!");
}
});
});
JSON响应如下,我试图抓住ApprovalCounter
的值:
{
"d":{
"__metadata":{
"uri":"http://collaboration/sites/TestWF/_vti_bin/listdata.svc/ResumeBank(84)",
"etag":"W/\"12\"",
"type":"Microsoft.SharePoint.DataService.ResumeBankItem",
"edit_media":"http://collaboration/sites/TestWF/_vti_bin/listdata.svc/ResumeBank(84)/$value",
"media_src":"http://collaboration/sites/TestWF",
"content_type":"text/xml"
},
"Id":84,
"ContentTypeID":"0x0120D52000E387E9726C57FE40807A71CC05BEF45A005D5A666FE9576E42B629AF7CDB33F1B0",
"ContentType":"JobApplication",
"Created":"\/Date(1413213951000)\/",
"CreatedBy":{
"__deferred":{
"uri":"http://collaboration/sites/TestWF/_vti_bin/listdata.svc/ResumeBank(84)/CreatedBy"
}
},
"CreatedById":1,
"Modified":"\/Date(1413561633000)\/",
"ModifiedBy":{
"__deferred":{
"uri":"http://collaboration/sites/TestWF/_vti_bin/listdata.svc/ResumeBank(84)/ModifiedBy"
}
},
"ModifiedById":1,
"CopySource":null,
"ApprovalStatus":"2",
"ApproverComments":null,
"Path":"/sites/TestWF/ResumeBank",
"CheckedOutTo":{
"__deferred":{
"uri":"http://collaboration/sites/TestWF/_vti_bin/listdata.svc/ResumeBank(84)/CheckedOutTo"
}
},
"CheckedOutToId":null,
"Name":"Tabitha Johnson",
"VirusStatus":"",
"IsCurrentVersion":true,
"Owshiddenversion":12,
"Version":"1.0",
"Title":"Tabitha Johnson",
"Description":null,
"RoleAppliedFor":"HR Assistant",
"HiringDepartment":{
"__deferred":{
"uri":"http://collaboration/sites/TestWF/_vti_bin/listdata.svc/ResumeBank(84)/HiringDepartment"
}
},
"HiringDepartmentValue":"HR",
"FirstRoundApproval":null,
"StartDate":"\/Date(1413158400000)\/",
"ApprovalCounter":0,
"Education":null,
"Experience":null,
"ApproveEachDoc":null,
"ApproveDocSet1st":"2",
"ApproveDocSet2nd":"2"
}
}
答案 0 :(得分:2)
它应该只是data.d.ApprovalCounter
答案 1 :(得分:1)
问题出在您的$.each()
函数中。
jQ.each(data.d.results, function(i, result) { ... }
$。each()正在寻找一个迭代的数组,然后是一个回调作为参数。
您的数组是嵌套在“d”对象中的所有json对象。那很好。
您的回调指定了一个“键值”对function(i, result)
,当循环到达{ "ApprovalCounter" : "0" }
对象时,i =“审批计数器”,结果=“0”。 ......所以results.Anything
会引发错误。
长话短说:
将其更改为:
JQ.each(data.d.results, function(results)) {
//...
var count = results.ApprovalCounter;
//...
}
它应该有用......
<强>可替换地:强>
您根本不需要使用$ .each()循环来访问“ApprovalCounter”中的值。
data.d.ApprovalCounter
...应该返回“0。”
希望这有帮助!