JavaScript access JSON message -
i found lot of ways access json data in javascript none of them seems working (for me):
ws.onmessage = function(msg) { var stringmsg = msg.data.tostring(); stringmsg = '\''+ stringmsg + '\'' console.log(stringmsg); var jsonmsg = json.parse(stringmsg); alert(jsonmsg.sensorid); var outputtextarea = document.getelementbyid("outputtext"); outputtextarea.value += (msg.data + "\n"); outputtextarea.scrolltop = outputtextarea.scrollheight; }
the console output displays this:
'{"sensorid": "kali1", "msgvalue": "aa:aa:aa:aa:aa:aa:bla!box bla wlan 4000", "msgtype": "unknown ssid"}'
and parser puts out error:
syntaxerror: json.parse: unexpected character @ line 1 column 1 of json data
but string seems valid json (thats think , json-validators used online)
i tried access message directly with:
alert(msg.sensorid) alert(msg.data.sensorid)
these popups display "undefined"
p.s. dont want convert json string , back, debugging
stringmsg = '\''+ stringmsg + '\''
you wrapping json single quotes. makes invalid json. don't that.
Comments
Post a Comment