So you are working with JSON, you get a JSON message and you want to retrieve some values from it. How?
Let’s suppose you get something like (this is an example take from here):
"{\"port\":1,\"counter\":39,\"payload_raw\":\"KAA=\",\"payload_fields\":{\"errorCode\":0,\"numberOfCycles\":40},\"metadata\":{\"time\":\"2017-01-15T22:55:04.204820257Z\",\"frequency\":868.1,\"modulation\":\"LORA\",\"data_rate\":\"SF7BW125\",\"coding_rate\":\"4/5\",\"gateways\":[{\"gtw_id\":\"eui-b827ebffffc19ca8\",\"timestamp\":4113032806,\"time\":\"1754-08-30T22:43:41.128654848Z\",\"channel\":0,\"rssi\":-90,\"snr\":8,\"latitude\":51.46018,\"longitude\":5.61902,\"altitude\":10}]}}"
The backslashes, the escape characters, are only for C# to know which double quotes are part of the message. It’s not necessary but you can remove them and then you have:
{"port":1,"counter":39,"payload_raw":"KAA=","payload_fields":{"errorCode":0,"numberOfCycles":40},"metadata":{"time":"2017-01-15T22:55:04.204820257Z","frequency":868.1,"modulation":"LORA","data_rate":"SF7BW125","coding_rate":"4/5","gateways":[{"gtw_id":"eui-b827ebffffc19ca8","timestamp":4113032806,"time":"1754-08-30T22:43:41.128654848Z","channel":0,"rssi":-90,"snr":8,"latitude":51.46018,"longitude":5.61902,"altitude":10}]}}
Well, it’s always good to check the message is proper JSON. You can check it with a ‘tool’ like http://jsonlint.com/
This is a valid message, if we format the structure, it looks a lot better:
{ "port": 1, "counter": 504, "payload_raw": "+QA=", "payload_fields": { "errorCode": 0, "numberOfCycles": 249 }, "metadata": { "time": "2017-01-10T23:31:06.087189682Z", "frequency": 868.1, "modulation": "LORA", "data_rate": "SF7BW125", "coding_rate": "4/5", "gateways": [{ "gtw_id": "eui-b827ebffffc19ca8", "gtw_trusted": true, "timestamp": 3771642998, "time": "1754-08-30T22:43:41.128654848Z", "channel": 0, "rssi": -80, "snr": 9, "latitude": 51.46018, "longitude": 5.61902, "altitude": 10}] } }
There are several ways to get values from this JSON message.
Doorgaan met het lezen van “Decoding and parsing JSON using NewtonSoft”