用JavaScript解码有效载荷

发布于 2025-01-26 03:03:54 字数 1262 浏览 3 评论 0原文

我需要一些支持,以用JavaScript解析有效载荷。在下面,您可以看到JSON的身体。我只需要返回ValueType“居住者”。

{"id":"8684bc99-5a69-4b7a-92c2-69faa91b668b","type":"airthings-webhook-cloudevent-occupancy-ventilation-sample-feed","source":"https://dashboard.airthings.com/integrations/webhooks/2c823594-3c4b-4352-9929-8486a5f8a985","dataContentType":"application/json","labels":{},"data":[{"measurementSystem":"METRIC","serialNumber":"2930118961","recorded":"2022-05-04T08:55:00","occupants":4.0,"ventilationAmount":171.0,"ventilationState":true,"ratings":{},"sensorUnits":{"occupants":"occ","ventilationAmount":"m3h","relativeVentilationRate":"ach"}}],"time":"2022-05-04T08:58:20.322709","specVersion":"0.2"}

我以以下解码器返回“数据”数组。现在,我需要数据中的对象“乘员”。有人可以告诉我我需要在以下代码中调整什么吗?

function Decoder(request) {

    // Parse JSON into Object
    var payload = JSON.parse(request.body);
    
    // Load Data from JSON
    var data = payload.data;
    var Occupancy = data.occupants;
    //var humidity = data.hum;
    //var battery = data.bat;
    
    // Forward Data into Device using Serial
    return data;
  
    
}

i need some support in parsing a payload with javascript. Below you see the json body. I need to return only the valuetype "occupants".

{"id":"8684bc99-5a69-4b7a-92c2-69faa91b668b","type":"airthings-webhook-cloudevent-occupancy-ventilation-sample-feed","source":"https://dashboard.airthings.com/integrations/webhooks/2c823594-3c4b-4352-9929-8486a5f8a985","dataContentType":"application/json","labels":{},"data":[{"measurementSystem":"METRIC","serialNumber":"2930118961","recorded":"2022-05-04T08:55:00","occupants":4.0,"ventilationAmount":171.0,"ventilationState":true,"ratings":{},"sensorUnits":{"occupants":"occ","ventilationAmount":"m3h","relativeVentilationRate":"ach"}}],"time":"2022-05-04T08:58:20.322709","specVersion":"0.2"}

I made it to return the "data" array with the following decoder. Now i need the object "occupants" within data. Can someone tell me what I need to adjust in the following code?

function Decoder(request) {

    // Parse JSON into Object
    var payload = JSON.parse(request.body);
    
    // Load Data from JSON
    var data = payload.data;
    var Occupancy = data.occupants;
    //var humidity = data.hum;
    //var battery = data.bat;
    
    // Forward Data into Device using Serial
    return data;
  
    
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文