【问题标题】:PugJS not interpolating numbers from json [duplicate]PugJS没有从json插入数字[重复]
【发布时间】:2021-07-05 06:40:49
【问题描述】:

我正在尝试呈现从路由器 API 获得的接口列表。它被格式化为 JSON。 Pug 只渲染字符串而不是数字。

输出(在 html 中正确呈现):

em1 192.168.0.0/24
Address: 192.168.0.1    
Received Packets: NaN    
Sent Packets: NaN

JSON:

(em1) / 192.168.0.1":{"name":"em1","flags":"0x8943","network":"192.168.0.0/24","address":"192.168.0.1","received-packets":2524179,"received-bytes":247344321,"sent-packets":2453592,"sent-bytes":1695092680},"

哈巴狗代码:

div.container
        each interface in interfaces
            div.interface
                h2.interface-name
                    | #{interface.name} #{interface.network}
                p Address: #{interface.address}
                p Received Packets: #{interface.received-packets}
                p Sent Packets: #{interface.sent-packets}

在我的一生中,我找不到任何可以告诉我如何处理本地数字的东西。我正在使用 nodejs 并用 pug 表达。

【问题讨论】:

  • 标签名称中的破折号(接收字节、发送数据包等)似乎引起了问题。如果我手动重新格式化 JSON 以删除破折号,它将呈现数字。但是 api 调用带有破折号。我真的必须格式化 json 来删除破折号吗?

标签: javascript node.js json express pug


【解决方案1】:

好的,回答我自己的问题。标识符名称中的破折号似乎不起作用。为了访问该项目,请在其周围加上括号和单引号。所以以下工作:

  div.interface
                h2.interface-name
                    | #{interface.name} #{interface.network}
                p Address: #{interface.address}
                p Received Packets: 
                    |#{interface['received-packets']}
                p Received Bytes: #{interface['received-bytes']}
                p Sent Packets: #{interface['sent-packets']}

从:Unable to access JSON property with "-" dash得到这个

【讨论】:

    猜你喜欢
    • 2018-03-09
    • 1970-01-01
    • 1970-01-01
    • 2011-04-24
    • 2020-06-21
    • 1970-01-01
    • 1970-01-01
    • 2018-08-06
    • 2022-08-16
    相关资源
    最近更新 更多