在不知道 sessionStorage 的键名的情况下访问 JSON 对象的元素

Accessing elements of JSON object without knowing the key names from sessionStorage

{
  "profile": {
    "_elapsedTime": "243",
    "code": "00",
    "data": {
      "acctNr": 3962480,
      "acctStusCd": "A",
      "acctTyp": "C",
      "addresses": [
        {
          "addrCityNm": "DO NOT SHIP",
          "addrCntryCd": "US",
          "addrLine1Txt": "DO NOT SHIP",
          "addrLocTyp": "HOME",
          "addrStCd": "IL",
          "addrZipCd": "60053"
        }
      ],
      "apptDt": "2004-09-02T00:00:00",
      "autoDbtCd": " ",
      "brthdyDt": "1911-11-11T00:00:00",
      "btyAdvrLvlTyp": "0",
      "campaignEndDt": "2017-02-17T00:59:59",
      "campaigns": [
        {
          "cmpgnNr": 5,
          "cmpgnYrNr": 2017,
          "mrktId": 75,
          "success": false
        },
        {
          "cmpgnNr": 6,
          "cmpgnYrNr": 2017,
          "mrktId": 75,
          "success": false
        },
        {
          "cmpgnNr": 4,
          "cmpgnYrNr": 2017,
          "mrktId": 75,
          "success": false
        }
      ],
      "currBalAmt": 0,
      "currSlsCmpgnNr": 5,
      "currSlsYrNr": 2017,
      "currentDtTime": "2017-01-30T14:48:29",
      "daysLeft": "16",
      "deliveryTypeList": [
        {
          "deliveryFromDt": "2017-01-30T14:48:29",
          "deliveryToDt": "2017-02-16T00:00:00",
          "deliveryType": "REG",
          "deliveryTypeLabel": "Regular"
        },
        {
          "deliveryFromDt": "2017-01-30T14:48:29",
          "deliveryToDt": "2017-02-01T14:48:29",
          "deliveryType": "SEP",
          "deliveryTypeLabel": "Separate"
        }
      ],
      "districtNumber": "5999",
      "divisionNumber": "594",
      "dstrbtnCntrCd": "018",
      "emailAddrTxt": "julie.silsby@avon.com",
      "evngPhonNr": "0000000000",
      "flexOrderingEndDate": "2017-02-21T23:59:59",
      "frstNm": "TEST",
      "initCmpgnNr": 18,
      "initCmpgnYrNr": 2004,
      "instntCrdtStusCd": "Y",
      "labcInd": "\u0000",
      "langCd": "E",
      "lastNm": "ACCOUNT",
      "ldrShipLvl": "X",
      "ldrshpTyp": "X",
      "loaNr": 324,
      "mainFrameInd": true,
      "mgrPrsnlRepAcctInd": "N",
      "natnlFutrCmpgnOrdInd": "N",
      "onlnOrdSbmsnDt": "2017-02-15T23:59:59",
      "opportunity": [
        {
          "cmpgn": {
            "cmpgnNr": 6,
            "cmpgnYrNr": 2017,
            "mrktId": 75,
            "success": false
          },
          "cmpgnNr": 6,
          "cmpgnYrNr": 2017,
          "futureCampaignEndDate": "2017-02-22T01:00:00",
          "itemCampaignList": [
            {
              "cmpgnNr": 6,
              "cmpgnYrNr": 2017,
              "mrktId": 75,
              "success": false
            },
            {
              "cmpgnNr": 5,
              "cmpgnYrNr": 2017,
              "mrktId": 75,
              "success": false
            },
            {
              "cmpgnNr": 4,
              "cmpgnYrNr": 2017,
              "mrktId": 75,
              "success": false
            }
          ],
          "onlnOrdSbmsnDt": "2017-03-03T00:59:59",
          "opptId": "5999",
          "opptTyp": "REG",
          "ordTyp": "REG",
          "rpsCmpgnEndDt": "2017-03-03T00:59:59",
          "rpsCmpgnStrtDt": "2017-02-17T01:00:00",
          "rpsOrdDueDt": "2017-03-01T23:59:59",
          "rpsShpngDt": "2017-03-02T00:00:00",
          "slngOprtntyTyp": "REG",
          "success": false
        },
        {
          "cmpgn": {
            "cmpgnNr": 4,
            "cmpgnYrNr": 2017,
            "mrktId": 75,
            "success": false
          },
          "cmpgnNr": 4,
          "cmpgnYrNr": 2017,
          "futureCampaignEndDate": "2016-12-18T01:00:00",
          "itemCampaignList": [
            {
              "cmpgnNr": 4,
              "cmpgnYrNr": 2017,
              "mrktId": 75,
              "success": false
            },
            {
              "cmpgnNr": 3,
              "cmpgnYrNr": 2017,
              "mrktId": 75,
              "success": false
            }
          ],
          "onlnOrdSbmsnDt": "2017-01-19T00:59:59",
          "opptId": "5999",
          "opptTyp": "REG",
          "ordTyp": "REG",
          "rpsCmpgnEndDt": "2017-01-19T00:59:59",
          "rpsCmpgnStrtDt": "2017-01-05T01:00:00",
          "rpsOrdDueDt": "2017-01-17T23:59:59",
          "rpsShpngDt": "2017-01-18T00:00:00",
          "slngOprtntyTyp": "REG",
          "success": false
        },
        {
          "cmpgn": {
            "cmpgnNr": 5,
            "cmpgnYrNr": 2017,
            "mrktId": 75,
            "success": false
          },
          "cmpgnNr": 5,
          "cmpgnYrNr": 2017,
          "futureCampaignEndDate": "2017-01-13T01:00:00",
          "itemCampaignList": [
            {
              "cmpgnNr": 5,
              "cmpgnYrNr": 2017,
              "mrktId": 75,
              "success": false
            },
            {
              "cmpgnNr": 4,
              "cmpgnYrNr": 2017,
              "mrktId": 75,
              "success": false
            },
            {
              "cmpgnNr": 3,
              "cmpgnYrNr": 2017,
              "mrktId": 75,
              "success": false
            }
          ],
          "onlnOrdSbmsnDt": "2017-02-17T00:59:59",
          "opptId": "5999",
          "opptTyp": "REG",
          "ordTyp": "REG",
          "rpsCmpgnEndDt": "2017-02-17T00:59:59",
          "rpsCmpgnStrtDt": "2017-01-19T01:00:00",
          "rpsOrdDueDt": "2017-02-15T23:59:59",
          "rpsShpngDt": "2017-02-16T00:00:00",
          "slngOprtntyTyp": "REG",
          "success": false
        }
      ],
      "opsCampaignStrtDt": "2017-01-13T01:00:00",
      "pastDueStusCd": "0",
      "payByChkStusCd": "Y",
      "prsdntClubStusCd": " ",
      "prsnlWebPage": "Y",
      "pswrdHint": "password2",
      "qpPinAvlbltyInd": false,
      "repAcsInd": "N",
      "repClubLvlCd": "DEFAULT",
      "repClubTypes": [
        {
          "prsdntClubInd": "Y",
          "repClubTypCd": "DEFAULT"
        }
      ],
      "repCustCount": 3,
      "repEnrlments": [
        {
          "enrldTyp": "PBLREL",
          "enrlmtStusCd": "N"
        },
        {
          "enrldTyp": "ACC",
          "enrlmtStusCd": "Y"
        },
        {
          "enrldTyp": "EREP",
          "enrlmtStusCd": "Y"
        }
      ],
      "repEnrollmentType": "E",
      "repMgrEmail": "5999@avon.com",
      "repPblctyRelInd": "N",
      "repPrfrdPrvwInd": "N",
      "rpsCd": "Z",
      "rpsCmpgnEndDt": "2017-02-17T00:59:59",
      "rpsCmpgnStrtDt": "2017-01-19T01:00:00",
      "rpsOrdDueDt": "2017-02-15T23:59:59",
      "rpsShpngDt": "2017-02-16T00:00:00",
      "sdrshipLvl": "X",
      "shpngFcltyCd": "015",
      "ssn": "1111",
      "tierCd": "U",
      "timeZone": "EST",
      "trckRouteCd": "HOLD",
      "trndsttrInd": "N",
      "vanityName": "jsilsby13"
    },
    "mrktCd": "US",
    "success": true,
    "timeStamp": "2017-01-30T14:48:29",
    "token": "71464bf401c522a91e859d4c15d22e570ee50e311e23b610a61b6f76a011c296935e1349afd54057efa3a8a7b85391b79570c628197f2066"
  }
}

这是我要进入 sessionStorage 的 JSON 对象。在控制台 > 应用程序 > 会话存储中,我看到了这个键——ngStorage-getRepInfo,后面是上面代表值的片段。

基本上我不确定我会如何抓住这个名字。我查看了 DOM,但在任何地方都没有看到这个 ngStorage-getRepInfo 标记,所以我假设它是动态的 (Angular)。

下面我将此语法用作占位符。

{}.profile.data.vanityName

我想要做的是获取这些值之一并将其显示在 DOM(没有 jQuery)中。

事实证明,我假设您将无法访问 'ngStorage-getRepInfo' 是不正确的。

您必须执行以下操作:

var session = sessionStorage.getItem("ngStorage-getRepInfo"),
    obj = JSON.parse(session);

The JSON.parse() method parses a JSON string, constructing the JavaScript value or object described by the string. An optional reviver function can be provided to perform a transformation on the resulting object before it is returned.

然后像任何其他对象一样向下钻取:

console.log(obj.profile.data.vanityName); //returns vanityName value