将所有传入的JSON消息存储在数组中,并使用javascript进行检索

问题描述 投票:0回答:2

我的应用程序每10分钟从另一个应用程序接收JSON数据。我需要存储传入的JSON消息以及何时收到新消息。我需要根据两条消息中的数据进行比较并进行计算。我该怎么做?

我这样尝试过(只是测试-没有实际数据):

var inputData = []
inputData = JSON.stringify(data1)
jsonArray.push(inputData)
var jsonArray = []
inputData = JSON.stringify(data2)
jsonArray.push(inputData)

for (var i = 0; i < jsonArray.length; i++) {
        jsonData = JSON.parse(jsonArray[i])
        if ((data3.x === jsonData.x))
        {    
            document.write("\n")
            document.write(jsonData.x);
        }
}

但是我的问题是,每当收到新消息时,都会调用此模块,并且会初始化jsonArray。那么,当出现新消息时,如何使数组或(先前的JSON消息)在模块中可用?代码中是否需要第二个数组?非常感谢您的帮助。

这里是传入消息的示例:

data1 = {"Battery":4.28,"moisture":15.88,"temperature":28,"messageType":"MOIST_TEMP","deviceId":"A371A","timestamp":"2019-12-02T02:53:25.000Z"}
javascript arrays json
2个回答
0
投票

对于这些类型的问题,您需要问自己步骤是什么?

让我们写下来:

  1. 初始化数据数组。由于尚未加载任何数据,因此它只是一个空数组。
  2. 具有在间隔上设置的功能以调用某个端点
  3. 如果第二点的请求通过,请仔细查看新接收到的数据,并与我们先前接收到的数据进行一些比较。
  4. 将数据添加到数组中

粗略地讲。我实际上只是拿上面的清单并做评论。从那里,我将在下面的代码中执行必要的操作。

// 1. initialize array of data. Since no data has been loaded, it's just an empty array.
const received = [];
// 2. Have a function set on an interval make a call to some endpoint
const interval = setInterval(async () => {

  // 3. If the request from point #2 goes through, look through the newly received data and do some comparisons with our previously received data.

  const data = await fetch('https://example.com/').then(res => res.json());
  for (let incomin_data of data) {
    for (let received_data of received) {
      // do your comparisons of stuff here....  
    }
  }

  // 4. add the data to the array
  received.push(...data);

}, 10000); // remember, 10000 ms = 10 seconds

小注释:上面的代码循环遍历新接收到的信息中的所有数据所有先前接收到的数据。尽管对于少量数据而言,这并不是一个大问题,但如果完全不考虑它,理论上您可能会冻结浏览器。更改for循环结构,以确保您遍历两个完整数据集的循环尽可能少。无论哪种情况,当它都是10行时,这都没有多大意义,但是如果您要提取大量数据,则接收到的数组可能会很快变得非常大,这可能会导致问题。

编辑

概念证明

const received = [];

const interval = setInterval(()=> {
  received.push(received.length);
  
  console.log(received);
}, 1500);

0
投票

我认为您可以通过这种方式希望对您有所帮助

//Suppose the Incoming Message is this
let incomingMessage = { "Battery": 9.780, "moisture": 75.88, "temperature": 36, "messageType": "MOIST_TEMP", "deviceId": "A371A", "timestamp": "2019-12-02T02:53:25.000Z" }

// The JsonArray you have right Now which stores the incoming Message
let jsonArray = [
    { "Battery": 4.28, "moisture": 15.88, "temperature": 28, "messageType": "MOIST_TEMP", "deviceId": "A371A", "timestamp": "2019-12-02T02:53:25.000Z" },
    { "Battery": 9.00, "moisture": 77.88, "temperature": 32, "messageType": "MOIST_TEMP", "deviceId": "A371A", "timestamp": "2019-12-02T02:53:25.000Z" },
    { "Battery": 4.28, "moisture": 15.88, "temperature": 28, "messageType": "MOIST_TEMP", "deviceId": "A371A", "timestamp": "2019-12-02T02:53:25.000Z" }
]

let isExist = []

//check that incoming message exits in the jsonArray 
for (let i = 0; i < jsonArray.length; i++) {
    let found = true;
    for (let p in incomingMessage) {
        if (incomingMessage.hasOwnProperty(p)) {
            if (jsonArray[i][p] !== incomingMessage[p]) {
                found = false;
                break;
            }
        }
    }
    isExist.push(found)
}
//Push if Incoming message doesn't exist
if(isExist.indexOf(true)<=-1){
 jsonArray.push(incomingMessage)
}
console.log(jsonArray)
© www.soinside.com 2019 - 2024. All rights reserved.