我無法從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"
}
}
你問的是如何解析JSON本身(如'JSON.parse')?我認爲jQuery在交出數據之前會自動解析JSON。 – Whymarrh 2014-10-17 21:31:23
沒有'd.results'你只想'data.d.ApprovalCounter'而不使用''' – charlietfl 2014-10-17 21:33:39
對不起,我的意思是檢索JSON對象的屬性。 – Athapali 2014-10-17 21:34:01