CSharp · Console
using System;
using JsonSubTypes;
using Newtonsoft.Json;
namespace ConsoleApp1
{
public class Program
{
public static void Main(string[] args)
{
var jsonText = @"{
""version"": 0.6,
""generator"": ""Overpass API 0.7.55.7 8b86ff77"",
""osm3s"": {
""timestamp_osm_base"": ""2019-05-21T18:03:02Z"",
""copyright"": ""The data included in this document is from www.openstreetmap.org. The data is made available under ODbL.""
},
""elements"": [
{
""type"": ""node"",
""id"": 4949106384,
""lat"": 32.2686857,
""lon"": -107.738218,
""tags"": {
""highway"": ""turning_circle""
}
},
{
""type"": ""way"",
""id"": 14527404,
""nodes"": [
142882281,
3048075541,
1598998260
],
""tags"": {
""highway"": ""residential"",
""name"": ""West Apple Street"",
""tiger:cfcc"": ""A41"",
""tiger:county"": ""Luna, NM"",
""tiger:name_base"": ""Apple"",
""tiger:name_direction_prefix"": ""W"",
""tiger:name_type"": ""St"",
""tiger:reviewed"": ""no""
}
}
]
}";
var json = JsonConvert.DeserializeObject<OSMdata>(jsonText);
foreach (Element jsonElement in json.elements)
{
Console.WriteLine(jsonElement.GetType());
}
}
}
public class OSMdata
{
public float version { get; set; }
public string generator { get; set; }
public Osm3s osm3s { get; set; }
[JsonConverter(typeof(JsonSubtypes), "type")]
public Element[] elements { get; set; }
}
public class Osm3s
{
public DateTime timestamp_osm_base { get; set; }
public string copyright { get; set; }
}
[JsonConverter(typeof(JsonSubtypes), "type")]
[JsonSubtypes.KnownSubType(typeof(Node), "node")]
[JsonSubtypes.KnownSubType(typeof(Edge), "way")]
public abstract class Element : OSMdata
{
public abstract string type { get; }
}
public class Node : Element
{
public override string type { get { return "node"; } }
public long id { get; set; }
public float lat { get; set; }
public float lon { get; set; }
public NodeTags tags { get; set; }
}
public class NodeTags
{
public string highway { get; set; }
public string _ref { get; set; }
}
public class Edge : Element
{
public override string type { get { return "way"; } }
public long id { get; set; }
public long[] nodes { get; set; }
public EdgeTags tags { get; set; }
}
public class EdgeTags
{
public string highway { get; set; }
public string name { get; set; }
public string cfcc { get; set; }
public string county { get; set; }
public string oneway { get; set; }
}
}