How to clear special characters from the expression Document.getElementById("Humidity"). value;

Asked

Viewed 71 times

1

I’m riding my own payload but he’s coming like this:

{
    "mode": "sync",
    "messageType": "57cd765743f3f9c473d5",
    "messages": [{
        "Humidity": "99.1",
        "Temperature": "99.2",
        "Brightness": "99.3",
        "Timestamp": "1999999999"
    }]
}

When you should go like this:

{
    "mode": "sync",
    "messageType": "57cd765743f3f9c473d5",
    "messages": [{
        "Humidity": 25.7,
        "Temperature": 21.5,
        "Brightness": 13.0,
        "timestamp": 1413100000
    }]
}

Code:

//Creates a new Messaging.Message Object and sends it to the HiveMQ MQTT Broker
var publish = function(payload, topic, qos) {

    mensagem.Humidity = document.getElementById("humidity").value;
    mensagem.Temperature = document.getElementById("temperature").value;
    mensagem.Brightness = document.getElementById("brightness").value;
    mensagem.Timestamp = document.getElementById("timestamp").value;

    pl.messages = new Array();
    pl.messages[0] = mensagem;

    console.log(JSON.stringify(pl));

    //Send your message (also possible to serialize it as JSON or protobuf or just use a string, no limitations)
    var message = new Messaging.Message(payload);
    message.destinationName = topic;
    message.qos = 2;

    //message.retained = true;
    client.send(message);
}

How do I clean up those "" leftover in mine payload?

  • 3

    What do you mean? Do you want to send an invalid JSON? Because the first value is correct, there are no "quotes left". The second is invalid if you want to test: https://jsonlint.com/

  • I do not understand... because mode" cannot contain the first " to be "mode"?

  • Nilson, thanks for your return... The problem is a little bigger than this one. I created a very simple app with 4 screen fields and 4 buttons. The general idea is to put the data on the screen, connect me with my MQTT CLOUD, and the mqtt cloud will save the inputted data on the screen to a table in my SAP HANA database... Testing via the web, when I put the payload below, data is recorded correctly in the table of my SAP HANA database: {mode":"Sync","messageType":"57cd765743f3f9c473d5","messages":[{"Humidity":25.7, "Temperature": 21.5, "Brightness": 13.0, "timestamp":1413100000}]}

  • Nilson, thanks for your return... The problem is a little bigger than this one. I created a very simple app with 4 screen fields and 4 buttons. The general idea is to put the data on the screen, connect me to my MQTT CLOUD, and the mqtt cloud will save the inputted data on the screen to a table in my SAP HANA database... Testing via the web: putting the payload 2 (generated by my app), records the data in my table ...

  • Maybe because you are sending the numbers as string. "foo": "32" and "foo": 32 are different things. I doubt very much that it has to do with quotation marks.

  • Hummm, but how do I change it? Can you tell me?

Show 1 more comment

2 answers

2

The attribute value of an HTML element is always string.

All you need to do is convert to float or int according to your need.

Example:

var input_int = document.getElementById('int').value;
var input_float = document.getElementById('float').value;
var input_string = document.getElementById('string').value;

// Todos são strings
console.log({
  "input_int": input_int,
  "input_float": input_float,
  "input_string": input_string
});

// Converte o que é número para o tipo correto
input_int = parseInt(input_int, 10);
input_float = parseFloat(input_float);

// Com os tipos corretos, o JSON é criado corretamente
console.log({
  "input_int": input_int,
  "input_float": input_float,
  "input_string": input_string
});
<input type="number" id="int" value="5" />
<br>
<input type="number" id="float" value="2.5" />
<br>
<input type="text" id="string" value="string" />

  • Thanks for the return Fernando :)

1

HTML field values are strings. The resulting JSON to call JSON.stringify with these values will actually be a JSON whose values will have double quotes.

If you do not want your JSON to contain strings, you must convert its values to the numeric type of Javascript. In your case, the timestamp is integer, so you can use the function parseInt javascript:

mensagem.Timestamp = parseInt(document.getElementById("timestamp").value);

The other values are not integer. For them you use the function parseFloat:

mensagem.Humidity = parseFloat(document.getElementById("humidity").value);
mensagem.Temperature = parseFloat(document.getElementById("temperature").value);
mensagem.Brightness = parseFloat(document.getElementById("brightness").value);
  • Can use a Number(document.getElementById("brightness").value) also :)

  • Renan! It worked! I can now save my screen data to my SAP HANA database (trial) table!! Thank you so much!!

Browser other questions tagged

You are not signed in. Login or sign up in order to post.