javascript - 无法循环遍历 NodeJS 中的 JSON 对象

标签 javascript json node.js

我有一个 NODEJS 程序,它使用 xml2js 将 XML 文件转换为 JSON 并解析它。然后我尝试遍历 json 对象并显示每个对象的 ID、LastReportTime,但我得到的输出显示未定义

输出

2015-02-26T18:45:35.34-0500 [App/0]   OUT BESAPI
2015-02-26T18:45:35.34-0500 [App/0]   OUT Computer
2015-02-26T18:45:35.34-0500 [App/0]   OUT Computer:undefined
2015-02-26T18:45:35.34-0500 [App/0]   OUT Done

Node

var fs = require('fs'),
    xml2js = require('xml2js');

var parser = new xml2js.Parser();
fs.readFile('myfile.xml', function(err, data) {
    parser.parseString(data, function (err, result) {

        var jsoniem = JSON.stringify(result);
        console.log(jsoniem);
        var data = JSON.parse(jsoniem);

        for (var obj in data) {
          if (data.hasOwnProperty(obj)) {
            console.log(obj);
            console.log("\n \n");

            if (obj == "BESAPI") {

              for (var prop in data[obj]) {
                console.log(prop);
                if (prop == "Computer") {
                   console.log(prop + ':' + data[obj][prop].ID);
                   console.log(prop + ':' + data[obj][prop].LastReportTime);
                }

              }

            } 

          }

        }

        console.log('Done');
    });

Json(程序从XML转JSON后)

{
    "BESAPI": {
        "$": {
            "xmlns:xsi": "http://www.w3.org/2001/XMLSchema-instance",
            "xsi:noNamespaceSchemaLocation": "BESAPI.xsd"
        },
        "Computer": [
            {
                "$": {
                    "Resource": "api/computer/2431038"
                },
                "LastReportTime": [
                    "Thu, 26 Feb 2015 14:54:41 +0000"
                ],
                "ID": [
                    "2431038"
                ]
            },
            {
                "$": {
                    "Resource": "api/computer/16710075"
                },
                "LastReportTime": [
                    "Thu, 26 Feb 2015 14:45:18 +0000"
                ],
                "ID": [
                    "16710075"
                ]
            },
            {
                "$": {
                    "Resource": "api/computer/3415985"
                },
                "LastReportTime": [
                    "Thu, 26 Feb 2015 14:50:52 +0000"
                ],
                "ID": [
                    "3415985"
                ]
            }
        ]
    }
}

XML

<?xml version="1.0" encoding="UTF-8"?>
<BESAPI xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="BESAPI.xsd">
    <Computer Resource="api/computer/2431038">
        <LastReportTime>Thu, 26 Feb 2015 14:54:41 +0000</LastReportTime>
        <ID>2431038</ID>
    </Computer>
    <Computer Resource="api/computer/16710075">
        <LastReportTime>Thu, 26 Feb 2015 14:45:18 +0000</LastReportTime>
        <ID>16710075</ID>
    </Computer>
    <Computer Resource="api/computer/3415985">
        <LastReportTime>Thu, 26 Feb 2015 14:50:52 +0000</LastReportTime>
        <ID>3415985</ID>
    </Computer>
</BESAPI>

最佳答案

考虑到您的 JSON 示例遍历对象似乎无关紧要。也不需要先对数据进行字符串化,然后再解析字符串。

fs.readFile('myfile.xml', function(err, data) {
  parser.parseString(data, function (err, result) {

    var jsoniem = JSON.stringify(result);
    console.log(jsoniem);

    result.BESAPI.Computer.forEach(function (el) {
      // Output arrays
      console.log(el.ID);
      console.log(el.LastReportTime);

      // Get first elements
      console.log(el.ID[0]);
      console.log(el.LastReportTime[0]);
    });

  }

  console.log('Done');
});

关于javascript - 无法循环遍历 NodeJS 中的 JSON 对象,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28755072/

相关文章:

javascript - 如何在多个按钮中使用相同的功能以获得相同的结果?

ios - 如何在 Swift 3 中使用 SwiftyJSON 搜索 JSON 元素

node.js - 如何使用node.js的ffi将输入键发送到窗口

node.js - Salesforce 不遵守提示选项

JavaScript 启动和停止计时

javascript - 使用 D3 库进行动态过滤

json - ELM 0.19 解码中超过 8 个映射

angularjs - 不支持 user.json 中的 “id”(数字)类型。使用 json-server 时使用对象或对象数组错误,为什么?

javascript - 遍历数组在每个元素上运行异步任务

javascript - 在 d3 节点上调用 if…then 函数