gpt4 book ai didi

javascript - 使用 javascript 和 php 分解 JSON 字符串并将值输入到 MYSQL

转载 作者:行者123 更新时间:2023-12-02 21:40:30 24 4
gpt4 key购买 nike

我已经阅读了一些问题,但它们要么是为不同的语言编写的,要么是为了在网页中解决而编写的,

我使用 node-red 接收 BLE 数据,但数据来自 1 个长字符串,我收到以下内容:

string
[{"timestamp":"2020-02-
24T13:44:57Z","type":"Gateway","mac":"AC233FC02D95","gatewayFree":96,"gatewayLoad":0.08}
{"timestamp":"2020-02-24T13:44:59Z","type":"iBeacon","mac":"DB1265E96B6F","bleName":"","ibeaconUuid":"ACFD065EC3C011E39BBE1A514 932AC01","ibeaconMajor":1,"ibeaconMinor":2,"rssi":-51,"ibeaconTxPower":-54,"battery":0}
{"timestamp":"2020-02-24T13:44:59Z","type":"iBeacon","mac":"F0728DEB0B9B","bleName":"","ibeaconUuid":"ACFD065EC3C011E39BBE1A514932AC01","ibeaconMajor":5,"ibeaconMinor":1,"rssi":-30,"ibeaconTxPower":-62,"battery":0}]

我的目标是以以下格式将数据输入到node-red

网关-Mac、IbeaconMinor、mac、rssi

我之前确实使用过不同的收集器来收集数据,它给了我以下格式的字符串

$GPRP,AC233FC02D95,FE2279DCB92B,-70,0201061AFF4C000215ACFD065EC3C011E39BBE1A514932AC0100050003C2

它单独输入收集的BLE设备数据,而不是在一个长的JSON字符串中,因此更容易转换和输入。

所以我的问题是我怎样才能改变它

string
[{"timestamp":"2020-02-
24T13:44:57Z","type":"Gateway","mac":"AC233FC02D95","gatewayFree":96,"gatewayLoad":0.08}
{"timestamp":"2020-02-24T13:44:59Z","type":"iBeacon","mac":"DB1265E96B6F","bleName":"","ibeaconUuid":"ACFD065EC3C011E39BBE1A514 932AC01","ibeaconMajor":1,"ibeaconMinor":2,"rssi":-51,"ibeaconTxPower":-54,"battery":0}
{"timestamp":"2020-02-24T13:44:59Z","type":"iBeacon","mac":"F0728DEB0B9B","bleName":"","ibeaconUuid":"ACFD065EC3C011E39BBE1A514932AC01","ibeaconMajor":5,"ibeaconMinor":1,"rssi":-30,"ibeaconTxPower":-62,"battery":0}]

string
[{"timestamp":"2020-02-
24T13:44:57Z","type":"Gateway","mac":"AC233FC02D95","gatewayFree":96,"gatewayLoad":0.08}

{2020-02-24T13:44:59Z,DB1265E96B6F,ACFD065EC3C011E39BBE1A514932AC01",1,2,-51,-54,0}

{2020-02-24T13:44:59Z,F0728DEB0B9B,ACFD065EC3C011E39BBE1A514932AC01,5,1,-30,-62,0}]

或者我如何以以下格式将其输入到mysql中作为 mysql 字符串

AC233FC02D95,F0728DEB0B9B51,1,-30 

AC233FC02D95,DB1265E96B6F,2,-51

在更改硬件之前,我像这样输入了字符串,但是使用新的 json 格式,我不确定要在代码中更改什么

GPRP,AC233FC02D95,FE2279DCB92B,-70,0201061AFF4C000215ACFD065EC3C011E39BBE1A514932AC0100050003C2

var raw = msg.payload;
msg.length = raw.length;
msg.raw = raw;
var data = {};

data.major = raw.slice(-10,-4);
data.mac = raw.substring(5,17);
data.hostname = raw.substring(18,30);
data.minor = raw.slice(-8,-4);
data.rssi = raw.substring(31,34)


var str = data.hostname;
var location = str.replace(`AC233FC02D95`, `PURPLE`)



var beacon = parseInt(data.minor ,16);



var msg = {
topic : "INSERT INTO `test`.`track` (`location`,`beacon`, `mac`,`rssi`)
VALUES ('"+location+"','"+beacon+"', '"+data.mac+"','"+data.rssi+"');"
}
return msg;

最佳答案

只需将带有字符串负载的消息传递给 JSON 节点,它就会将其解析为 JSON 对象,然后您就可以正常访问字段。

尽管从您发布的内容来看,数组条目似乎缺少 , 分隔符。

从那里您可以构建您想要的任何字符串或将字段直接插入数据库。

关于javascript - 使用 javascript 和 php 分解 JSON 字符串并将值输入到 MYSQL,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60377601/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com