I've a json file with coordinates [x,y] I would like to do query based on the coordinates but i did not know how to transfer this to c# classes. I tried using https://json2csharp.com/ but it gives me the classes like this
public class DatasetData
{
public int id { get; set; }
public int channel_id { get; set; }
public string location { get; set; }
public int speed_1 { get; set; }
public int speed_2 { get; set; }
public int speed_3 { get; set; }
}
public class Root
{
public List<DatasetData> dataset_data { get; set; }
}
This is the json. As you see I've inside the location; type and coordinates but i don't know to extract them to classes and using them.
{
"dataset_data":[
{
"id":1234,
"channel_id":2,
"location":"{\"type\":\"Point\",\"coordinates\":[-1.17273271,51.9132423]}",
"speed_1":1,
"speed_2":2,
"speed_3":3
}
]
}
CodePudding user response:
The problem with your json is that the location
is not an object rather than a string. So, you need to deserialize that as well.
If you define your Location
class as Lasse V. Karlsen suggested:
public class Location
{
public string type { get; set; }
public List<double> coordinates { get; set; }
}
Then all you need to do is to modify your DatasetData
class like this:
public class DatasetData
{
...
public string location { set; private get;}
public Location Location => JsonConvert.DeserializeObject<Location>(location);
...
}
- The
location
property is write only from the deserialization perspective - The
Location
property is read only from the deserialization perspective