簡體   English   中英

如何在 Java 腳本中解析嵌套的 Json

[英]How to Parse nested Json In Java script

我在 js 中有這個 json,我試圖從“數據”Object 中獲取密鑰,例如 SessionId 和 Email。

請幫忙...

{
  "HasError": false,
  "Errors": [],
  "ObjectName": "WebCheckoutCreateSessionResponse",
  "Data": {
    "HasError": false,
    "ReturnCode": 0,
    "ReturnMessage": null,
    "SessionId": "abcde",
    "SessionUrl": "https://pci.aaa.com/WebCheckout/Angular/Checkout.html#!/ZCreditWebCheckout/55cf7d1306b2bcc15d791dde524d2b4f616421172e6d75a013597c8dd2668843",
    "RequestData": {
      "Key": "ad",
      "Local": "He",
      "UniqueId": "<InvNo>1705</InvNo><InvYear>3102</InvYear><SugMismah>15</SugMismah><key>ad</key>",
      "SuccessUrl": "",
      "CancelUrl": "",
      "CallbackUrl": "",
      "PaymentType": "regular",
      "CreateInvoice": false,
      "AdditionalText": "",
      "ShowCart": true,
      "ThemeColor": "005ebb",
      "Installments": {
        "Type": "regular",
        "MinQuantity": 1,
        "MaxQuantity": 12
      },
      "Customer": {
        "Email": "someone@gmail.com",
        "Name": "Demo Client",
        "PhoneNumber": "077-3233190",
        "Attributes": {
          "HolderId": "none",
          "Name": "required",
          "PhoneNumber": "required",
          "Email": "optional"
        }
      },
      "CartItems": [
        {
          "Amount": 117,
          "Currency": "US",
          "Name": "Danny ",
          "Description": "Hi ",
          "Quantity": 1,
          "Image": "https://www.aaa.com/site/wp-content/themes/z-credit/img/decisions/decision2.png",
          "IsTaxFree": false
        }
      ],
      "GetCurrencyCode": "376",
      "BitButtonEnabled": true,
      "MaxPayButtonEnabled": true,
      "ApplePayButtonEnabled": true,
      "GooglePayButtonEnabled": true,
      "ShowTotalSumInPayButton": true
    }
  },
  "ResponseType": 0
}
const population = JSON.parse(xhr.responseText);
      for (const key in population) {
        if (population.hasOwnProperty(key)) {
          console.log(`${key}: ${population[key]}`);
        }
      }

您忘記為Data屬性編制索引。

const population = JSON.parse(xhr.responseText).Data;

for (const key in population) {
  console.log(`${key}: ${population[key]}`);
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM