获取javascript中最后一次出现的json元素的值

时间:2016-08-05 22:57:09

标签: javascript json

http://hubtest.atspace.cc/json.html

在上面的JSON响应中,我需要获取LAST objectId值以用作另一个URL的一部分。用javascript做这个的最好方法是什么?

我尝试使用正则表达式,但它太乱了。我假设有一种更简单的方法。

2 个答案:

答案 0 :(得分:3)

在源代码上尝试JSON.parse(...)(因为它实际上是JSON),然后将其视为对象文字。

由于它是一个数组,您可以var array = JSON.parse(...)然后获得array[array.length - 1].objectID

答案 1 :(得分:1)

基本上,

var lastObj = a[a.length - 1];

返回数组中的最后一个对象。然后,您可以访问其属性;

var objectID = lastObj['objectId'];

快捷方式

var objectID = a[a.length - 1]['objectId'];

Array.length返回数组中的总元素,并且因为数字是从零开始的,所以从长度上取1可获得最后一个索引。例如。对于具有6个元素的数组,最后一个元素索引是5(6-1)= 5)

工作片段,我将对象剪裁为2。



   var a = [{
       "type": "PAPER",
       "data": null,
       "proceedingNumber": "IPR2016-00546",
       "petitionId": "1463786",
       "mimeType": null,
       "fileName": "'306 Petition.pdf",
       "partyGroupType": "petitioner",
       "proceedingPartyGroupId": null,
       "availability": "PUBLIC",
       "documentName": "Petition For Inter Partes Review U.S. Patent No. 8,772,306",
       "pageCount": "0",
       "documentType": "16",
       "exhibitNumber": null,
       "petitionVO": null,
       "institutionDecisionVO": null,
       "terminationDecisionVO": null,
       "proceedingReqType": null,
       "proceedingReqTypeId": null,
       "proceedingReqTypeStatusId": null,
       "appealId": null,
       "internalUserProxyEmail": null,
       "proceedingId": "1463786",
       "paperType": "16",
       "documentTypeId": 16,
       "customMotionTypeName": null,
       "otherMotionType": null,
       "objectId": "d29ya3NwYWNlOi8vU3BhY2VzU3RvcmUvNGVjOGFjMzQtODI5Yi00OTZhLTg0ZDItMDU2NTQzNmQ4NTI0OzEuMA==",
       "objectType": null,
       "artifactSubmissionId": "84644821",
       "exhibitSequenceNumber": null,
       "dateAdded": "02/02/2016",
       "uploadStatus": null,
       "expungedFlag": "N",
       "deletedFlag": null,
       "docVersionLabel": null,
       "filingDate": "02/02/2016",
       "proceedingArtifactId": "169264898",
       "artifactNumber": "1",
       "petitionState": null,
       "patentNumber": null,
       "fileSize": 0,
       "submitterId": 11915,
       "comment": null,
       "createdbyAuthorName": null,
       "disableSelect": null,
       "employeeId": null,
       "lockControlNo": 0,
       "paperTypeName": "Petition",
       "proceedingPartyId": null,
       "filingParty": "petitioner",
       "documentCategory": null,
       "showExpungeAction": false,
       "showUnExpungeAction": false,
       "showDownloadLink": true,
       "skipUploadTaskRecord": false,
       "showEditLink": true,
       "inputStream": null
   }, {
       "type": "PAPER",
       "data": null,
       "proceedingNumber": "IPR2016-00546",
       "petitionId": "1463786",
       "mimeType": null,
       "fileName": "306 Exhibit List.pdf",
       "partyGroupType": "petitioner",
       "proceedingPartyGroupId": null,
       "availability": "PUBLIC",
       "documentName": "Exhibit List",
       "pageCount": "0",
       "documentType": "16",
       "exhibitNumber": null,
       "petitionVO": null,
       "institutionDecisionVO": null,
       "terminationDecisionVO": null,
       "proceedingReqType": null,
       "proceedingReqTypeId": null,
       "proceedingReqTypeStatusId": null,
       "appealId": null,
       "internalUserProxyEmail": null,
       "proceedingId": "1463786",
       "paperType": "16",
       "documentTypeId": 16,
       "customMotionTypeName": null,
       "otherMotionType": null,
       "objectId": "d29ya3NwYWNlOi8vU3BhY2VzU3RvcmUvNDBhMjRjMmQtYWZkZi00OTdlLThkN2ItZGQ2ZTE5MmVjMWVkOzEuMA==",
       "objectType": null,
       "artifactSubmissionId": "84644822",
       "exhibitSequenceNumber": null,
       "dateAdded": "02/02/2016",
       "uploadStatus": null,
       "expungedFlag": "N",
       "deletedFlag": null,
       "docVersionLabel": null,
       "filingDate": "02/02/2016",
       "proceedingArtifactId": "169264900",
       "artifactNumber": "2",
       "petitionState": null,
       "patentNumber": null,
       "fileSize": 0,
       "submitterId": 11915,
       "comment": null,
       "createdbyAuthorName": null,
       "disableSelect": null,
       "employeeId": null,
       "lockControlNo": 0,
       "paperTypeName": "Petition",
       "proceedingPartyId": null,
       "filingParty": "petitioner",
       "documentCategory": null,
       "showExpungeAction": false,
       "showUnExpungeAction": false,
       "showDownloadLink": true,
       "skipUploadTaskRecord": false,
       "showEditLink": true,
       "inputStream": null
   }]
console.log(a[a.length - 1]['objectId']);