private void Parse(NSData data) { try { var earthquakes = new List <Earthquake> (); var dump = JsonValue.Parse(NSString.FromData(data, NSStringEncoding.UTF8)) as JsonObject; JsonValue featureCollection = dump ["features"]; for (int i = 0; i < MaximumNumberOfEarthquakesToParse; i++) { var currentEarthquake = new Earthquake(); var earthquake = featureCollection [i] as JsonObject; JsonValue earthquakeProperties = earthquake ["properties"]; currentEarthquake.Magnitude = NSNumber.FromFloat((float)earthquakeProperties ["mag"]); currentEarthquake.USGSWebLink = new NSString((string)earthquakeProperties ["url"]); currentEarthquake.Location = new NSString((string)earthquakeProperties ["place"]); //date and time in milliseconds since epoch var seconds = (Int64)earthquakeProperties ["time"]; var date = new DateTime(1970, 1, 1, 0, 0, 0).AddMilliseconds(seconds); string str = date.ToString("yyyy'-'MM'-'dd'T'HH':'mm':'ss'Z'"); currentEarthquake.Date = dateFormatter.Parse(str); JsonValue earthquakeGeometry = earthquake ["geometry"]; var coordinates = earthquakeGeometry ["coordinates"] as JsonArray; currentEarthquake.Longitude = NSNumber.FromFloat(coordinates [0]); currentEarthquake.Latitude = NSNumber.FromFloat(coordinates [1]); if (earthquakes.Count > SizeOfEarthquakesBatch) { AddEarthquakesToList(earthquakes); earthquakes.Clear(); } else { earthquakes.Add(currentEarthquake); } } if (earthquakes.Count > 0) { AddEarthquakesToList(earthquakes); } } catch (Exception e) { Console.WriteLine(e.StackTrace + e.Message); var userInfo = NSDictionary.FromObjectAndKey(new NSString("Error while parsing GeoJSON"), NSError.LocalizedDescriptionKey); var parsingError = new NSError(new NSString(), 0, userInfo); InvokeOnMainThread(new Selector("HandleEarthquakesError:"), parsingError); } }
private void Parse (NSData data) { try { var earthquakes = new List<Earthquake> (); var dump = JsonValue.Parse (NSString.FromData (data, NSStringEncoding.UTF8)) as JsonObject; JsonValue featureCollection = dump ["features"]; for (int i = 0; i < MaximumNumberOfEarthquakesToParse; i++) { var currentEarthquake = new Earthquake (); var earthquake = featureCollection [i] as JsonObject; JsonValue earthquakeProperties = earthquake ["properties"]; currentEarthquake.Magnitude = NSNumber.FromFloat ((float)earthquakeProperties ["mag"]); currentEarthquake.USGSWebLink = new NSString ((string)earthquakeProperties ["url"]); currentEarthquake.Location = new NSString ((string)earthquakeProperties ["place"]); //date and time in milliseconds since epoch var seconds = (Int64)earthquakeProperties ["time"]; var date = new DateTime (1970, 1, 1, 0, 0, 0).AddMilliseconds (seconds); string str = date.ToString ("yyyy'-'MM'-'dd'T'HH':'mm':'ss'Z'"); currentEarthquake.Date = dateFormatter.Parse (str); JsonValue earthquakeGeometry = earthquake ["geometry"]; var coordinates = earthquakeGeometry ["coordinates"] as JsonArray; currentEarthquake.Longitude = NSNumber.FromFloat (coordinates [0]); currentEarthquake.Latitude = NSNumber.FromFloat (coordinates [1]); if (earthquakes.Count > SizeOfEarthquakesBatch) { AddEarthquakesToList (earthquakes); earthquakes.Clear (); } else { earthquakes.Add (currentEarthquake); } } if (earthquakes.Count > 0) AddEarthquakesToList (earthquakes); } catch (Exception e) { Console.WriteLine (e.StackTrace + e.Message); var userInfo = NSDictionary.FromObjectAndKey (new NSString ("Error while parsing GeoJSON"), NSError.LocalizedDescriptionKey); var parsingError = new NSError (new NSString (), 0, userInfo); InvokeOnMainThread (new Selector ("HandleEarthquakesError"), parsingError); } }