用json键中的:解析nodejs中的json

时间:2019-06-26 05:38:31

标签: node.js json

我的nodejs代码从xml中的HTTP POST调用接收到响应。我使用xml2js将其转换为json,现在我需要阅读此信息以获得json密钥之一的数据。

这是我的json数据的一部分的外观。我正在尝试阅读以下内容:

var base64encoded = jsonxml."soapenv:Body".runReportResponse.runReportReturn.reportBytes;

但是,当我运行此程序时,我收到错误消息:

jsonxml."soapenv:Body".runReportResponse.runReportReturn.reportBytes;
                                        ^^^^^^^^^^^^^^
SyntaxError: Unexpected string.'

我也尝试过删除双引号,但随后它给出现在数据中的冒号(:)赋予了例外。如何读取此类数据?

整个json:

[
  {
    "Envelope": {
      "$": {
        "xmlns:soapenv": "http://schemas.xmlsoap.org/soap/envelope/",
        "xmlns:xsd": "http://www.w3.org/2001/XMLSchema",
        "xmlns:xsi": "http://www.w3.org/2001/XMLSchema-instance"
      },
      "Body": {
        "runReportResponse": {
          "$": {
            "xmlns": "http://xmlns.oracle.com/oxp/service/PublicReportService"
          },
          "runReportReturn": {
            "metaDataList": {
              "$": {
                "xsi:nil": "true"
              }
            },
            "reportBytes": "MzAwMDAwMDA0Mzk5ODEwLERDT0cgQ29ycG9yYXRlIEJVDQozMDAwMDAwMDk0ODE4MzEsREVMRlRMQUJfVVNfQlVTSU5FU1NfVU5JVA0KMzAwMDAwMDAzMDYyNTI1LERFTEhJVEVDSF9VU19CVVNJTkVTU19VTklUDQozMDAwMDAwMDMwNjE1ODMsREVMTFMgVVMgQlUNCjMwMDAwMDAwMzE3OTE0NixERUxNRkcgVVMgQlUNCjMwMDAwMDAxMDI1NDA1NyxESEMgQ29ycG9yYXRlDQo=",
            "reportContentType": "text/plain;charset=UTF-8",
            "reportFileID": {
              "$": {
                "xsi:nil": "true"
              }
            },
            "reportLocale": {
              "$": {
                "xsi:nil": "true"
              }
            }
          }
        }
      }
    }
  }
]

1 个答案:

答案 0 :(得分:0)

我已经根据您的新JSON更新了答案(谢谢!)。看来问题在于JSON中的每个属性实际上都是一个数组。

如果您不希望这种行为,建议您看看xml2js options 对象中的explicitArray选项,将其设置为 false 。  例如:

explicitArray (default: true): Always put child nodes in an array if true; otherwise an array is created only if there is more than one.

这可以简化访问属性!

let response = [
  {
"Envelope": {
  "$": {
    "xmlns:soapenv": "http://schemas.xmlsoap.org/soap/envelope/",
    "xmlns:xsd": "http://www.w3.org/2001/XMLSchema",
    "xmlns:xsi": "http://www.w3.org/2001/XMLSchema-instance"
  },
  "Body": {
    "runReportResponse": {
      "$": {
        "xmlns": "http://xmlns.oracle.com/oxp/service/PublicReportService"
      },
      "runReportReturn": {
        "metaDataList": {
          "$": {
            "xsi:nil": "true"
          }
        },
        "reportBytes": "MzAwMDAwMDA0Mzk5ODEwLERDT0cgQ29ycG9yYXRlIEJVDQozMDAwMDAwMDk0ODE4MzEsREVMRlRMQUJfVVNfQlVTSU5FU1NfVU5JVA0KMzAwMDAwMDAzMDYyNTI1LERFTEhJVEVDSF9VU19CVVNJTkVTU19VTklUDQozMDAwMDAwMDMwNjE1ODMsREVMTFMgVVMgQlUNCjMwMDAwMDAwMzE3OTE0NixERUxNRkcgVVMgQlUNCjMwMDAwMDAxMDI1NDA1NyxESEMgQ29ycG9yYXRlDQo=",
        "reportContentType": "text/plain;charset=UTF-8",
        "reportFileID": {
          "$": {
            "xsi:nil": "true"
          }
        },
        "reportLocale": {
          "$": {
            "xsi:nil": "true"
          }
        }
      }
    }
  }
}
  }
];


console.log("Report bytes: ", response[0].Envelope.Body.runReportResponse.runReportReturn.reportBytes);