作者热门文章
- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我正在尝试从我的 Cyberpower UPS 代理解析 JSON(用于将数据导入 InfluxDB/Grafana)。除了电池状态之外,我能够解析我想要的所有内容。如下所示,如果 UPS 与市电断开,状态为“Discharging”,但如果已连接,则该值包含一个逗号,“Normal, Fully Charged”....我无法找到一种方法来解析数据,因为我知道值中可能有也可能没有逗号。如果值为“Discharging”,那么它工作正常,但由于 “Normal, Fully Charged” 中的逗号,grep 结果为 “Normal(显然缺少结束语)。
单个值的 JSON:
"battery":{"state":"Discharging",
带逗号和空格的 JSON 值:
"battery":{"state":"Normal, Fully Charged",
我的 curl/greb。是否可以这样做来提取一个值,该值在我想要的值中可能有逗号,也可能没有逗号?如果是这样,我做错了什么?
curl http://10.0.1.61:3052/agent/ppbe.js/init_status.js | grep -oP '(?<="battery":{"state":)[^,]*' | head -1
放电时完整的 .js 页面:
var ppbeJsObj = {
"status": {
"communicationAvaiable": true,
"onlyPhaseArch": false,
"utility": {
"state": "Blackout",
"stateWarning": true,
"voltage": "0",
"frequency": "60.00",
"voltages": null,
"currents": null,
"frequencies": null,
"powerFactors": null
},
"bypass": {
"state": "Normal",
"stateWarning": false,
"voltage": null,
"current": null,
"frequency": null,
"voltages": null,
"currents": null,
"frequencies": null,
"powerFactors": null
},
"output": {
"state": "Normal",
"stateWarning": false,
"voltage": "120.0",
"frequency": null,
"load": 58,
"watt": 522,
"current": null,
"outputLoadWarning": false,
"outlet1": null,
"outlet2": null,
"activePower": null,
"apparentPower": null,
"reactivePower": null,
"voltages": null,
"currents": null,
"frequencies": null,
"powerFactors": null,
"loads": null,
"activePowers": null,
"apparentPowers": null,
"reactivePowers": null,
"emergencyOff": null,
"batteryExhausted": null
},
"battery": {
"state": "Discharging",
"stateWarning": true,
"voltage": null,
"capacity": 99,
"runtimeFormat": 1,
"runtimeFormatWarning": false,
"runtimeHour": 0,
"runtimeMinute": 20,
"chargetimeFormat": null,
"chargetimeHour": null,
"chargetimeMinute": null,
"temperatureCelsius": null,
"highVoltage": null,
"lowVoltage": null,
"highCurrent": null,
"lowCurrent": null
},
"upsSystem": {
"state": "Normal",
"stateWarning": false,
"temperatureCelsius": null,
"temperatureFahrenheit": null,
"maintenanceBreak": null,
"systemFaultDueBypass": null,
"systemFaultDueBypassFan": null,
"originalHardwareFaultCode": "0x8080"
},
"modules": null,
"deviceId": 0
}
};
最佳答案
这应该有效:
grep -P -o '(?<="battery":{"state":")[a-zA-Z, ]+'
问候!
编辑:根据 OP 评论进行编辑。
$ cat test.txt
var ppbeJsObj={"status":{"communicationAvaiable":true,
"onlyPhaseArch":false,
"utility":{"state":"Blackout",
"stateWarning":true,
"voltage":"0",
"frequency":"60.00",
"voltages":null,
"currents":null,
"frequencies":null,
"powerFactors":null},
"bypass":{"state":"Normal",
"stateWarning":false,
"voltage":null,
"current":null,
"frequency":null,
"voltages":null,
"currents":null,
"frequencies":null,
"powerFactors":null},
"output":{"state":"Normal",
"stateWarning":false,
"voltage":"120.0",
"frequency":null,
"load":58,
"watt":522,
"current":null,
"outputLoadWarning":false,
"outlet1":null,
"outlet2":null,
"activePower":null,
"apparentPower":null,
"reactivePower":null,
"voltages":null,
"currents":null,
"frequencies":null,
"powerFactors":null,
"loads":null,
"activePowers":null,
"apparentPowers":null,
"reactivePowers":null,
"emergencyOff":null,
"batteryExhausted":null},
"battery":{"state":"Discharging",
"stateWarning":true,
"voltage":null,
"capacity":99,
"runtimeFormat":1,
"runtimeFormatWarning":false,
"runtimeHour":0,
"runtimeMinute":20,
"chargetimeFormat":null,
"chargetimeHour":null,
"chargetimeMinute":null,
"temperatureCelsius":null,
"highVoltage":null,
"lowVoltage":null,
"highCurrent":null,
"lowCurrent":null},
"upsSystem":{"state":"Normal",
"stateWarning":false,
"temperatureCelsius":null,
"temperatureFahrenheit":null,
"maintenanceBreak":null,
"systemFaultDueBypass":null,
"systemFaultDueBypassFan":null,
"originalHardwareFaultCode":"0x8080"},
"modules":null,
"deviceId":0}};
$ grep -P -o '(?<="battery":{"state":")[a-zA-Z, ]+' test.txt
Discharging
关于javascript - 将 Cyberpower UPS 的 JSON 解析为 InfluxDB/Grafana,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48830617/
我是一名优秀的程序员,十分优秀!