2014-10-17 39 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