Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- case class LocationData(name: String, lat: Long, lng: Long)
- def parse4Square(jsonString: String): List[LocationData] = {
- val json = spray.json.JsonParser(jsonString)
- val items: List[JsObject] = json.asJsObject.fields("groups") match {
- // get array of groups
- case Some(JsArray(groups)) => groups flatMap { g =>
- // get array of items in each group and glue them with flatMap
- g.asJsObject.fields("items") match {
- case Some(JsArray(itemsArray)) => itemsArray.map(_.asJsObject())
- case _ => Nil
- }
- }
- case _ => Nil
- }
- // from every item
- items.flatMap(i => {
- // extract its venue
- i.fields.get("venue") flatMap { venue => {
- // extract name
- val venueObject = venue.asJsObject
- val name = venueObject.fields.get("name") match {
- case JsString(n) => n
- case _ => "untitled location"
- }
- venueObject.fields.get("location").flatMap(location => {
- // if venue, lat and long exists - create a LocationData case class
- List("lat", "lng").map(location.asJsObject.fields.get(_)) match {
- case List(Some(JsNumber(lat), JsNumber(lng))) => Some(LocationData(name, lat.toLong, lng.toLong))
- case _ => None
- }
- })
- }
- }
- })
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement