【问题标题】:Reading JsValue in Scala在 Scala 中读取 JsValue
【发布时间】:2016-05-02 23:13:19
【问题描述】:

如何获取第一位居民的姓名?这是 Json 文件。以下是什么?

val bigwig = (json \ "residents")(1)..... 


import play.api.libs.json._

val json: JsValue = Json.parse("""
{
  "name" : "Watership Down",
  "location" : {
    "lat" : 51.235685,
    "long" : -1.309197
  },
  "residents" : [ {
    "name" : "Fiver",
    "age" : 4,
    "role" : null
  }, {
    "name" : "Bigwig",
    "age" : 6,
    "role" : "Owsla"
  } ]
}
""")

【问题讨论】:

    标签: json scala playframework


    【解决方案1】:

    又快又脏(没有验证):

    ((jsonObject \ "residents").as[Seq[JsObject]].head \ "name").as[String]
    

    【讨论】:

      【解决方案2】:

      您可以通过几种方式做到这一点。

      类映射和直接 json 字段访问示例

      import play.api.libs.json.{JsError, JsSuccess, Json}
      
      case class JsonSchemaView(name: String, location: Location, residents: Seq[Resident])
      
      case class Location(lat: Double, long: Double)
      
      case class Resident(name: String, age: Int, role: Option[String])
      
      object Location {
        implicit val locationFormat = Json.format[Location]
      }
      
      object Resident {
        implicit val residentFormat = Json.format[Resident]
      }
      
      object JsonSchemaView {
        implicit val jsonSchemaViewFormat = Json.format[JsonSchemaView]
      }
      
      object Runner {
        def main (args: Array[String]) {
      
          val mySchemaView = JsonSchemaView("name", Location(40, -40), Seq(Resident("ress", 4, None), Resident("josh", 16, Option("teenager"))))
      
          val json = Json.toJson(mySchemaView)
      
          println(Json.prettyPrint(json))
      
          val myParsedSchema = Json.parse(json.toString).validate[JsonSchemaView]
      
          myParsedSchema match {
            case JsSuccess(schemaView, _) =>
              println(s"success: $schemaView")
            case error: JsError =>
              println(error)
          }
      
      
          //The hard way
          val jsonObject = Json.parse(json.toString())
      
          (jsonObject \ "name").validate[String] match {
            case JsSuccess(name, _) =>
              println(s"success: $name")
            case error: JsError =>
              println(error)
          }
      
          (jsonObject \ "residents").validate[Seq[JsObject]] match {
            case JsSuccess(residents, _) =>
              residents foreach { resident =>
                println((resident \ "age").as[Int]) //unsafe, using the wrong type will cause an exception
              }
            case error: JsError =>
              println(error)
            }
         } 
      }
      

      【讨论】:

        【解决方案3】:

        您的部分代码返回 Json,因此您可以使用 JSON basics 中的所有 Json 方法

        访问name字段使用路径:(...\"name)"

        要获取名称值,请使用as 方法:(...\"name").as[String]

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2018-01-03
          • 2014-09-23
          • 1970-01-01
          • 2017-01-07
          • 2012-12-26
          • 1970-01-01
          相关资源
          最近更新 更多