Parse get request to jsonarray in Scala

2019-06-08 02:37发布

The request https://api.github.com/users returns a list of users. How can I parse it in Scala to array of json objects so I can access objects' fields, e.g., login, id, ...

标签: json scala rest
1条回答
闹够了就滚
2楼-- · 2019-06-08 03:06

you can use sttp-client to make http calls and circe to decode json to scala object.

val SttpVersion = "1.1.12"
val circeVersion = "0.9.3"

libraryDependencies ++= Seq(
  "com.softwaremill.sttp" %% "core" % SttpVersion,
  "com.softwaremill.sttp" %% "async-http-client-backend-future" % SttpVersion,

  "io.circe" %% "circe-core" % circeVersion,
  "io.circe" %% "circe-generic" % circeVersion,
  "io.circe" %% "circe-parser" % circeVersion
)

Example,

object GetExample {

  import scala.concurrent.ExecutionContext.Implicits.global

  import com.softwaremill.sttp._
  import com.softwaremill.sttp.asynchttpclient.future._

  import io.circe._
  import io.circe.parser._

  implicit val backend = AsyncHttpClientFutureBackend()

  def main(args: Array[String]): Unit = {

    val usersResponse: Future[Response[String]] = sttp.get(uri"""https://api.github.com/users""").send()

    val users = usersResponse.map {
      case Response(Right(r), _, _, _, _) => parse(r).map { json =>
        val users: Option[Vector[Json]] = json.asArray.map(arr => arr.flatMap(_.\\("login")))
        users.getOrElse(Vector.empty[Json])
      }
      case Response(Left(l), _, _, _, _) => Vector.empty[Json]
    }

    println(users)
  }
}

you will result as below if you try on REPL

scala>     val users = usersResponse.map {
     |       case Response(Right(r), _, _, _, _) => parse(r).map { json =>
     |         val users: Option[Vector[Json]] = json.asArray.map(arr => arr.flatMap(_.\\("login")))
     |         users.getOrElse(Vector.empty[Json])
     |       }
     |       case Response(Left(l), _, _, _, _) => Vector.empty[Json]
     |     }
users: scala.concurrent.Future[Serializable with Equals] = Future(<not completed>)

scala> users
res1: scala.concurrent.Future[Serializable with Equals] = Future(Success(Right(Vector("mojombo", "defunkt", "pjhyett", "wycats", "ezmobius", "ivey", "evanphx", "vanpelt", "wayneeseguin", "brynary", "kevinclark", "technoweenie", "macournoyer", "takeo", "Caged", "topfunky", "anotherjesse", "roland", "lukas", "fanvsfan", "tomtt", "railsjitsu", "nitay", "kevwil", "KirinDave", "jamesgolick", "atmos", "errfree", "mojodna", "bmizerany"))))
查看更多
登录 后发表回答