Solved How to extract value from Websocket Json Message in QML
-
Hi, I am using websockets to link my front end QML GUI with a backend application and can't seem to understand how to extract a value of "Result" and store it in "var textresult" from the JSON message received from the application. Please help.
My Main.qml
WebSocket { id: socket url: "ws://localhost/wsapi" onTextMessageReceived: { console.log(message); var textresult = message; }
The output for console.log(message) reply from server:
qml: {"metadata": {}, "type": "connected", "define": "query"} qml: {"metatype":"query","data":{"query":["hello"]}} qml: {"metadata": {"query": "hello"}, "type": "connected", "define": null} qml: {"metadata": {"result": "Hello World"}, "type": "reply", "define": null} --> want to extract value of result "Hello World" from reply
With alot of searching and reading around I have managed to get the value of "result" but i still am facing the issue of additional incoming message lines which i am unable to filter out.
modified main.qml
onTextMessageReceived: { var somestring = [message] var jsonObject = JSON.parse(somestring) var inmessage = [JSON.stringify(jsonObject.metadata.result)] var inmessagearray = [].filter(function(n){ return n !== undefined }) inmessagearray.push(inmessage) var armessage = String(inmessagearray).replace(/^\s*\n/gm, ""); console.log(armessage); }
new console.log(armessage) output
The blank lines are Undefined incoming messages as they do not contain "result" and are being filtered out by the filter function.qml: qml: qml: "hello" qml: qml: "Hello World" qml: qml: qml: qml: qml: qml: qml: qml: qml: qml: qml: qml: qml: qml: qml: qml: qml: qml: qml: qml: qml:
Is there any resource or example that i can look up in regards to this ?
-
Not sure if this helps, but this is how I am handling JSON data returned from a Websocket.
My data is packaged by a PHP Websocket server as follows:
$wsOutData = array(
'type' => $type,
'text' => $wsTemp,
);$type is an identifier I use just to keep track of the data type.
$wsTemp is the actual JSON data returned from my PHP process that basically just grabs a MySQL record and does a PHP json_encode on it.
$wsOutData is encoded by the WS server...with 'action' and 'data' elements, where $wsOutData is packed into the 'data' element.
$wsOutData is sent to the QML client, who then does a JSON.parse() on the incoming packet. The packet is then processed according to the 'action' that was encoded into the packet by the Websocket server.
The 'data' portion of the packet (which is the $wsOutData record) is then passed off to the appropriate processing function. The processing function then does this:
var dataJson = JSON.parse(data['text']); \parse the 'text' item as a JSON object to a JS array
var dataStr = dataJson[0]; \now grab the parsed item (which is the base element in the array)From then on, I can access all 'columns' of the database records as elements in the JS array:
dataStr.username
dataStr.firstName
dataStr.lastNameHope this helps...
--Sam
-
Solved this using conditional if statements like this
var somestring = JSON.parse(message) var msgType = somestring.type; if (msgType === "result") { Someaction() }