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,
}
}
As i said in comment download Newtonsoft.Json
JObject data= JObject.Parse(data);
JToken details = data["details"];
string lat = details["telemetry"]["location"]["lat"].ToString();
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;
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.