Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/474.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 将Cyberpower UPS的JSON解析为XDB/Grafana_Javascript_Json_Bash_Influxdb_Grafana - Fatal编程技术网

Javascript 将Cyberpower UPS的JSON解析为XDB/Grafana

Javascript 将Cyberpower UPS的JSON解析为XDB/Grafana,javascript,json,bash,influxdb,grafana,Javascript,Json,Bash,Influxdb,Grafana,我试图从Cyberpower UPS代理解析JSON(用于将数据导入XDB/Grafana)。我能够解析除电池状态之外的所有我想要的东西。如下图所示,如果UPS与公用电源断开,则状态为“放电”,但如果UPS已连接,则该值包含一个逗号,“正常,完全充电”。。。。我无法找到解析数据的方法,因为我知道值中可能有逗号,也可能没有逗号。如果值为“放电”,则工作正常,但由于“正常,完全充电”中的逗号,grep结果为“正常”(显然缺少结束引号) 单一值的JSON: "battery":{"state":"Di

我试图从Cyberpower UPS代理解析JSON(用于将数据导入XDB/Grafana)。我能够解析除电池状态之外的所有我想要的东西。如下图所示,如果UPS与公用电源断开,则状态为“放电”,但如果UPS已连接,则该值包含一个逗号,“正常,完全充电”。。。。我无法找到解析数据的方法,因为我知道值中可能有逗号,也可能没有逗号。如果值为“放电”,则工作正常,但由于“正常,完全充电”中的逗号,grep结果为“正常”(显然缺少结束引号)

单一值的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
bash解决方案: 你可以这样做:

找到
状态
键/值:
grep-oP'(?这应该可以:

grep -P -o '(?<="battery":{"state":")[a-zA-Z, ]+'

grep-P-o'(?请参阅:(我相信这是同一个问题。)是否需要解析JSON(使用
jq'.battery.state'
)或者JavaScript。你给出的是一个JavaScript片段作为输出,这与JSON完全不同。@chepner-我相信这里的目的是从JavaScript中提取对象,然后解析它。这是UPS制造商的一个可怕的设计决策。@Eptescus-你想在bash中解析这个JavaScript吗?-这个might然后对您有用。()根据regexr.com,这是有效的,但是在我的服务器上执行操作时,不会返回任何内容。curl | grep-oP'(?@eptesicus添加-P和-o标志,如我的版本所示。
curl http://10.0.1.61:3052/agent/ppbe.js/init_status.js | \
grep -oP '(?<="battery":\s*{)([^}]+)(?=})' | \
grep -oP '(?<="state": ")([^"]+)(?=")'
"state": "Normal, Fully Charged",
"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
Normal, Fully Charged
curl http://10.0.1.61:3052/agent/ppbe.js/init_status.js | \
grep -oP '(?<="battery":\s*{)(([^{}]++|(?1))*)(?=})' | \
grep -oP '(?<="state": ")([^"]+)(?=")'
// Here 'input' is your 'init_status.js' string
input = JSON.parse(input.replace(
    /(?:^[\s\S]*?(?={))|(?:;[^;]*?$)/g,
    ""
))
const http = require('http');
http.get(
    'http://10.0.1.61:3052/agent/ppbe.js/init_status.js',
    response => {
        let body = '';

        // Read the data.
        response.on('data', chunk => {body += chunk});

        // The file is complete, now we can use it.
        response.on('end', () => {
            const data = JSON.parse(
                body.replace(
                    /(?:^[\s\S]*?(?={))|(?:;[^;]*?$)/g,
                    ""
                )
            );

            // This logs the entire JSON object.
            console.log(data);

            // Getting the 'battery state'
            console.log(data.status.battery.state);
        });
    }
);
grep -P -o '(?<="battery":{"state":")[a-zA-Z, ]+'
$ 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