Home > Net >  Parsing JSON data and display it as a table
Parsing JSON data and display it as a table

Time:04-22

  1. How do I to get the first timestamp value off of the first row using C# and Newtonsoft.Json?
  2. How do I display this data in a table with headers beaId, bfiId, timestamp, beaName, bfiName using AngularJS?
{
  "deviceId": "14164616421676286414",
  "data": {
    "65473": {
      "beaId": "14164616421676286414",
      "bfiId": 65473,
      "rssi": -94,
      "temp": 20.25,
      "battery": 3660,
      "timestamp": 1650558413362,
      "status": "CONNECTED",
      "beaName": "",
      "bfiName": "RO-EI-05:312_AC",
      "channel": 39
    },
    "65185": {
      "beaId": "14164616421676286414",
      "bfiId": 65185,
      "rssi": -94,
      "temp": 20.25,
      "battery": 3660,
      "timestamp": 1650558433304,
      "status": "CONNECTED",
      "beaName": "",
      "bfiName": "RO-CI-01:309_AC",
      "channel": 39
    },
    "65059": {
      "beaId": "14164616421676286414",
      "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

CodePudding user response:

You have to deserialise the JSON and than to operate with it. In C# you have build in functions to do this. deserialise JSON

CodePudding user response:

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:

Please post it as a separate question. It is prefered in StachOverflow that each posted question will contain 1 issue.

  • Related