在nodejs中解析API请求中的对象

时间:2016-10-03 07:59:29

标签: json node.js

我正在尝试使用nodejs对有关座位空缺的一些数据进行排序。 API的响应就像这样

{

    "data":[
        {
            "date":"2016-10-01",
            "timeslots":[
                {
                    "from_time":"10:00",
                    "to_time":"18:00",
                    "vacancies":2400
                },
                {
                    "from_time":"18:00",
                    "to_time":"21:00",
                    "vacancies":900
                }
            ]
        },
        {
            "date":"2016-10-02",
            "timeslots":[
                {
                    "from_time":"10:00",
                    "to_time":"18:00",
                    "vacancies":2400
                },
                {
                    "from_time":"18:00",
                    "to_time":"21:00",
                    "vacancies":900
                }
            ]
        },
        {
            "date":"2016-10-03",
            "timeslots":[
                {
                    "from_time":"10:00",
                    "to_time":"18:00",
                    "vacancies":2400
                },
                {
                    "from_time":"18:00",
                    "to_time":"21:00",
                    "vacancies":900
                }
            ]
        },
        {
            "date":"2016-10-04",
            "timeslots":[
                {
                    "from_time":"10:00",
                    "to_time":"18:00",
                    "vacancies":2400
                },
                {
                    "from_time":"18:00",
                    "to_time":"21:00",
                    "vacancies":900
                }
            ]
        },
        {
            "date":"2016-10-05",
            "timeslots":[
                {
                    "from_time":"10:00",
                    "to_time":"18:00",
                    "vacancies":2400
                },
                {
                    "from_time":"18:00",
                    "to_time":"21:00",
                    "vacancies":900
                }
            ]
        }
    ]

}

我正在尝试使用以下代码处理此问题

app.get('/fetchdata/:ticketid/:fromdate/:todate', function (req, res){
    var id = req.params.ticketid;
    var fromdate = req.params.fromdate;
    var todate = req.params.fromdate;
    var key = id+fromdate+todate;
    var username = 'foo';
    var password = 'foobar';
    var slot_url = "https://"+username+":"+password+"@boo.boobar.com/1/timeslots?productId="+id+"&fromDate="+fromdate+"&toDate="+todate;
    var result = {};
    process.env.NODE_TLS_REJECT_UNAUTHORIZED = "0";
    request({

        url: slot_url,
        json: true,
        headers: headers
    }, function (error, response, body) {
        if (!error && response.statusCode === 200) {
            var data = [];            
            try {

                body.forEach(function (entry) {

                    var temp = {};
                    temp['date'] = entry["date"];                    
                    data = data.concat(temp);

                });

                result['data'] = data;
                result['response'] = 1;
                result['message'] = 'Capacity list fetched successfully!';
                //client.set(key, JSON.stringify(data));
                res.json(result);
            }
            catch (err) {
                result['response'] = 0;
                result['message'] = 'No data found!';
                result['exception'] = err.message;
                res.json(result);
            }
        }
        else
        {
            console.log("Something went wrong!! "+error.message);
        }
    })
});

现在我不断收到回复{"response":0,"message":"No data found!","exception":"Object #<Object> has no method 'forEach'"}

我理解这是因为timeslots键中的data键是一个对象,但是我无法解析它。应该对我的代码进行哪些更改才能解析此内容。

我的最终目标是根据日期对这些数据进行分类并创建一个像这样的json

{
   "2016-10-01":[
      {
         "from_time":"10:00",
         "to_time":"10:30",
         "capacity":-147
      },
      {
         "from_time":"10:30",
         "to_time":"11:00",
         "capacity":1
      }
   ]
}

1 个答案:

答案 0 :(得分:1)

body是根对象,所以看起来你想要迭代body.data而不是(例如body.data.forEach(...)),其中一个数组