-1

I have this object and I am trying to get the lat and lon values in the location field within telemetry. I am new to c#, but know python. What I would have done in python was:

item.details["telemetry"].location.lat

Can someone please show me how this is supposed to be done in c#?

"details": {
            "asset": {
                "id": "5ca12266-35fe-4f75-8593-588fba777d6d",
                "name": "ZS-FOO"
            },
            "assetType": {
                "id": "87bc0a83-045d-4810-888c-237b5ef17ea4",
                "name": "FOO"
            },
            "telemetry": {
                "flags": 0,
                "ownerId": "4adc68e4-7113-4b0f-8aba-dea213e8a948",
                "originId": "09e0021f-9c54-425b-ae23-cbfe3c786a66",
                "type": "telemetry",
                "linked": ["5ca12266-35fe-4f75-8593-588fba777d6d"],
                "date": "2017/01/20 13:46:01",
                "received": "2017/01/20 13:46:21",
                "active": true,
                "location": {
                    "lon": 116072,
                    "lat": -87448,
                    "speed": 74,
                    "altitude": 98.228,
                    "heading": 56,
                    "accuracy": 5,
                    "age": 0
                },
                "zones": [],
                "routes": null,
                "state": null,
                "telemetry": {
                    "msg_type": 0,
                    "vert_speed": 1.2,
                    "hdop": 1.65,
                    "vdop": 3.51,
                    "movement": 1,
                    "odo_counter": 162704.12317,
                    "hours_00_counter": 1027.885442,
                    "idle_counter": 0
                },
                "io": null,
                "spd": null,
            }
        }
Harry
  • 13,091
  • 29
  • 107
  • 167
  • 1
    What you've shown here appears to be a json representation, which is not a natively supported data structure in C#. Can you provide details on the C# data structure into which this would be deserialized? – Mark Adelsberger Feb 06 '17 at 16:33
  • 2
    You'd want to use a JSON parser to parse the JSON into a dictionary or an object. – EvilTak Feb 06 '17 at 16:33
  • that came form this/: Console.WriteLine(JsonConvert.SerializeObject(item)); – Harry Feb 06 '17 at 16:35
  • Download Newtonsoft.Json dll to help you pares the jsons data. – Farzin Kanzi Feb 06 '17 at 16:38
  • Possible duplicate: http://stackoverflow.com/a/6620173/1638261 – jomsk1e Feb 06 '17 at 16:39
  • 2
    It should be `item.details.telemetry.location.lat`,depending on the names in your classes – Pikoh Feb 06 '17 at 16:40
  • Trying this: var telemetry = JObject.Parse(item.details["telemetry"]); But cannot convert object to string. But if it was an object, why cant iI do item.detials.telemetry? /Users/hermanstander/Projects/IndigoSAT/IndigoSAT/Program.cs(40,40): Error CS1061: Type `System.Collections.Generic.Dictionary' does not contain a definition for `telemetry' and no extension method `telemetry' of type `System.Collections.Generic.Dictionary' could be found. Are you missing an assembly reference? (CS1061) (IndigoSAT) – Harry Feb 06 '17 at 16:44
  • 1
    First,we must now what is your data origin. Is it Json? or you have an object and,for unknown reasons, you are mixing Json with all this? – Pikoh Feb 06 '17 at 16:47
  • Data in this scenario is Core.Streaming.ServerQueueMessage – Harry Feb 06 '17 at 17:09
  • This worked: var telemetry = JObject.Parse(item.details["telemetry"].ToString()); lat = telemetry["location"]["lat"], – Harry Feb 06 '17 at 17:16

2 Answers2

0

As i said in comment download Newtonsoft.Json

JObject data= JObject.Parse(data);
JToken details = data["details"];

string lat = details["telemetry"]["location"]["lat"].ToString();
Farzin Kanzi
  • 3,380
  • 2
  • 21
  • 23
0

Firstly, generate models for your data. You can do it manually by creating classes or if you are in Visual Studio, this would be helpful:

Edit=> Paste Special=> Paste JSON as class. This will generate the classes for you.

Afterwords, go with Json.NET or any other library to parse your file:

public class Program{ 
      static void Main (string[] args){
      var client = new WebClient();
      var jsonString = client.DownloadString("Your link to Json file");
      var response= JsonConvert.DeserializeObject</*Json Class*/>(jsonString);
      //Now you can retrieve the data like this:
      var lat = response.details.telemetry.location.lat;
    }
}

Don't forget to import:

using System.Net;
using Newtonsoft.Json;
Coke
  • 965
  • 2
  • 9
  • 22