从嵌套JSON获取值

时间:2019-03-18 06:48:40

标签: javascript node.js api

我对Node.js完全陌生。我必须从API响应的嵌套JSON中读取特定值。我试图弄清楚但得到的是Undefined而不是值。每当我尝试读取基础对象时,我都会得到响应的值,但是每当我尝试读取数组下的对象时,我都会得到未定义的值。

代码段:

var https = require('https');
var optionget = {
  host: 'api-dev.dataoverflow.com',
  port: 443,
  path: '/test1/test2/',
  method: 'GET',
  HEADERS: {
    'Authorization': 'Basic grege==',
    'X-PruAppID : '
    PlainCardPortal '
  }
};

console.info(optionsget)

var reqGet = htttps.request(optionsget, function(res) {
  console.log("statusCode: ", res.statusCode);
  res.on('data', function(d) {
    process.stdout.write(d);
    process.stdout.write(jsonobj);
    var Value = jsonobj.items.item.topping.type;
    console.log(Value)
  });
});

reqGet.end();
reqGet.on('error', function(e) {
  console.error(e);
});

var optionsgetmsg = {
  host: 'api-dev.dataoverflow.com',
  port: 443,
  method: 'GET'
};

console.info(optionsgetmsg);

var reqGet = https.request(optionsget, function(res) {
  console.log("statusCode: ", res.statusCode);
  res.setEncoding('utf-8')

  res.on('data', function(data) {
    process.stdout.write(data);
  });
});

reqGet.end();
reqGet.on('error', function(e) {
  console.error(e);
});

嵌套的JSON代码段:

{
    "items":
        {
            "item":
                [
                    {
                        "id": "0001",
                        "type": "donut",
                        "name": "Cake",
                        "ppu": 0.55,
                        "batters":
                            {
                                "batter":
                                    [
                                        { "id": "1001", "type": "Regular" },
                                        { "id": "1002", "type": "Chocolate" },
                                        { "id": "1003", "type": "Blueberry" },
                                        { "id": "1004", "type": "Devil's Food" }
                                    ]
                            },
                        "topping":
                            [
                                { "id": "5001", "type": "None" },
                                { "id": "5002", "type": "Glazed" },
                                { "id": "5005", "type": "Sugar" },
                                { "id": "5007", "type": "Powdered Sugar" },
                                { "id": "5006", "type": "Chocolate with Sprinkles" },
                                { "id": "5003", "type": "Chocolate" },
                                { "id": "5004", "type": "Maple" }
                            ]
                    },

                    ...

                ]
        }
}

我只想从此JSON中读取type : Glazed,但我却得到了Undefined

1 个答案:

答案 0 :(得分:1)

您必须遍历数组中的值。您无法使用.表示法访问它们。使用map遍历数组,并在顶部数组中输入它们的类型

var jsonobj = {
  "items": {
    "item": [{
        "id": "0001",
        "type": "donut",
        "name": "Cake",
        "ppu": 0.55,
        "batters": {
          "batter": [{
              "id": "1001",
              "type": "Regular"
            },
            {
              "id": "1002",
              "type": "Chocolate"
            },
            {
              "id": "1003",
              "type": "Blueberry"
            },
            {
              "id": "1004",
              "type": "Devil's Food"
            }
          ]
        },
        "topping": [{
            "id": "5001",
            "type": "None"
          },
          {
            "id": "5002",
            "type": "Glazed"
          },
          {
            "id": "5005",
            "type": "Sugar"
          },
          {
            "id": "5007",
            "type": "Powdered Sugar"
          },
          {
            "id": "5006",
            "type": "Chocolate with Sprinkles"
          },
          {
            "id": "5003",
            "type": "Chocolate"
          },
          {
            "id": "5004",
            "type": "Maple"
          }
        ]
      }
    ]
  }
}
var Value = jsonobj.items.item.map(e=>e.topping)[0].map(x=>x.type);
console.log(...Value)