The code below shows how I'm reading / deserializing a geoJSON file into an ExpandoObject using Newtonsoft.Json.
As I'm looping through the items how do I test if the item contains the attribute place?
Or can I do this in a LINQ query?
As can be seen I've tried several methods from this page none are working
var PPL = new List<string> { "city", "farm", "hamlet", "isolated_dwelling", "neighbourhood", "quarter", "suburb", "town", "village" };
....
JsonSerializer serializer = new JsonSerializer();
using (FileStream s = File.Open(jsonFile, FileMode.Open))
using (StreamReader sr = new StreamReader(s, Encoding.UTF8))
using (JsonReader reader = new JsonTextReader(sr))
{
while (reader.Read())
{
// deserialize only when there's "{" character in the stream
if (reader.TokenType == JsonToken.StartObject)
{
dynamic jsonFeatures = serializer.Deserialize<ExpandoObject>(reader);
foreach (var item in jsonFeatures.features)
{
if(HasAttr(item, "place"))
//if (((IDictionary<String, object>)item).ContainsKey("place"))
//if (item.ContainsKey("place"))
{
// Some fixed attributes
var osm_id = item.properties.osm_id ?? string.Empty;
string name = item.properties.name ?? string.Empty;
string is_in = item.is_in ?? string.Empty;
string Place = item.place ?? string.Empty;
string geom = JsonConvert.SerializeObject(item.geometry);
bool IsPPL = PPL.Contains(Place, StringComparer.OrdinalIgnoreCase);
if (IsPPL)
{
Console.WriteLine("name: {0} is_in: {1} Place: {2} geom: {3} ", name, is_in, Place, geom);
}
}
}
}
}
}
private static bool HasAttr(ExpandoObject expando, string key)
{
return ((IDictionary<string, Object>)expando).ContainsKey(key);
}