2014-10-17 73 views
0

我無法從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" 
    } 
} 
+1

你問的是如何解析JSON本身(如'JSON.parse')?我認爲jQuery在交出數據之前會自動解析JSON。 – Whymarrh 2014-10-17 21:31:23

+1

沒有'd.results'你只想'data.d.ApprovalCounter'而不使用''' – charlietfl 2014-10-17 21:33:39

+0

對不起,我的意思是檢索JSON對象的屬性。 – Athapali 2014-10-17 21:34:01

回答

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」。

希望這會有所幫助!

2

這應該只是data.d. ApprovalCounter