'Parsing JSON data and display it as a table
- How do I to get the first timestamp value off of the first row using C# and Newtonsoft.Json?
- How do I display this data in a table with headers beaId, bfiId, timestamp, beaName, bfiName using AngularJS?
{
"deviceId": "141646166286414",
"data": {
"65473": {
"beaId": "141646166286414",
"bfiId": 65473,
"rssi": -94,
"temp": 20.25,
"battery": 3660,
"timestamp": 1650558413362,
"status": "CONNECTED",
"beaName": "",
"bfiName": "RO-EI-05:312_AC",
"channel": 39
},
"65185": {
"beaId": "141646166286414",
"bfiId": 65185,
"rssi": -94,
"temp": 20.25,
"battery": 3660,
"timestamp": 1650558433304,
"status": "CONNECTED",
"beaName": "",
"bfiName": "RO-CI-01:309_AC",
"channel": 39
},
"65059": {
"beaId": "141646166286414",
"bfiId": 65059,
"rssi": -92,
"temp": 20.25,
"battery": 3660,
"timestamp": 1650558393482,
"status": "CONNECTED",
"beaName": "",
"bfiName": "RO-EI-09:399_AC",
"channel": 39
}
}
}
This is what I have tried. Let me know if this is the correct way to get the first timestamp value from the JSON string.
var beaInfo = JObject.Parse("<Above Json string>");
var sdmap = beaInfo["data"];
var tmStamp = sdmap.First.First["timestamp"];
TIA
Solution 1:[1]
Regarding the C# json parsing question:
The code you posted is correct.
And looks like you already use Newtonsoft.Json
. In your code tmStamp
will be of type JToken
and will hold the integer value of 1650558413362
.
Some explanations about using Newtonsoft.Json:
As you probably know json is a tree-like structure.
beaInfo
is a JObject
holding the entire json structure.
sdmap
is a JToken
and holds the sub tree "data".
First
returns a JToken
of the first child of a given node.
Using it twice will get tmStamp
to be the JToken
refering to the node that you need.
You can verify that tmStamp
indeed refers to the first timestamp you can use:
Console.WriteLine("timestamp: " + tmStamp.ToString());
Regarding the AngularJS question:
I recomend to post it as a separate question. It is prefered in StackOverflow (and related sites) that each post will contain only 1 question. See here: Can I ask only one question per post?
Solution 2:[2]
You have to deserialise the JSON and than to operate with it. In C# you have build in functions to do this. deserialise JSON
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
Solution | Source |
---|---|
Solution 1 | |
Solution 2 | chepilov |