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

Popular posts from this blog

Failed to execute goal org.apache.maven.plugins:maven-surefire-plugin:2.12:test (default-test) on project.Error occurred in starting fork -

windows - Debug iNetMgr.exe unhandle exception System.Management.Automation.CmdletInvocationException -

configurationsection - activeMq-5.13.3 setup configurations for wildfly 10.0.0 -