How to clean special characters from document.getElementById ("humidity"). value;

1

I am putting my payload but it is coming like this:

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

When should it 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 "" left over in my payload?

    
asked by anonymous 08.06.2017 / 15:35

2 answers

2

The value attribute of an HTML element is always string.

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

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" />
    
08.06.2017 / 16:38
1

HTML field values are strings. The resulting JSON of calling JSON.stringify with these values will actually be a JSON whose values will have double quotation marks.

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

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

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

mensagem.Humidity = parseFloat(document.getElementById("humidity").value);
mensagem.Temperature = parseFloat(document.getElementById("temperature").value);
mensagem.Brightness = parseFloat(document.getElementById("brightness").value);
    
08.06.2017 / 16:34