使用scala play-json 2.4.x,如何将json对象的名称提取到不同的对象中?
Using scala play-json 2.4.x, how do I extract the name of a json object into a different object?
鉴于此 json:
{
"credentials": {
"b79a2ba2-lolo-lolo-lolo-lololololol": {
"description": "meaningful description",
"displayName": "git (meaningful description)",
"fullName": "credential-store/_/b79a2ba2-lolo-lolo-lolo-lololololol",
"typeName": "SSH Username with private key"
}
},
"description": "Credentials that should be available irrespective of domain specification to requirements matching.",
"displayName": "Global credentials (unrestricted)",
"fullDisplayName": "Credentials » Global credentials (unrestricted)",
"fullName": "credential-store/_",
"global": true,
"urlName": "_"
}
和这个 Scala 目标 class:
case class JenkinsCredentials(uuid: String, description: String)
如何创建 Reads[JenkinsCredentials]
来提取第一个对象名称 uuid b79a2ba2-lolo-lolo-lolo-lololololol
以及描述?
根据文档,它应该是这样的:
implicit val credsReader: Reads[JenkinsCredentials] = (
(JsPath).read[String] and
(JsPath \ "description").read[String]
)(JenkinsCredentials.apply _)
与(Json.parse(content) \ "credentials").validate[Seq[JenkinsCredentials]
一起使用
但是文档没有讨论任何关于将对象的名称提取为在其他地方使用的字段的内容...
编辑:澄清
我的最终状态是从 json 对象而非数组解析的 Seq
of JenkinsCredentials
。由于 JSON 的结构。对于 [=37= 下的每个潜在凭证条目,我会将 "credentials": "UUID" 和 "credentials":"UUID":"description" 拉出到一个对象中]
我没有使用 Reads[T]
方法解决了这个问题:
//Get the key names inside of the credentials
(json \ "credentials").as[JsObject].fields.map{ case (credId, value) =>
JenkinsCredentials(credId, (value \ "description").validate[String].get)
}
这有效,但不验证位,也不使用转换器。
鉴于此 json:
{
"credentials": {
"b79a2ba2-lolo-lolo-lolo-lololololol": {
"description": "meaningful description",
"displayName": "git (meaningful description)",
"fullName": "credential-store/_/b79a2ba2-lolo-lolo-lolo-lololololol",
"typeName": "SSH Username with private key"
}
},
"description": "Credentials that should be available irrespective of domain specification to requirements matching.",
"displayName": "Global credentials (unrestricted)",
"fullDisplayName": "Credentials » Global credentials (unrestricted)",
"fullName": "credential-store/_",
"global": true,
"urlName": "_"
}
和这个 Scala 目标 class:
case class JenkinsCredentials(uuid: String, description: String)
如何创建 Reads[JenkinsCredentials]
来提取第一个对象名称 uuid b79a2ba2-lolo-lolo-lolo-lololololol
以及描述?
根据文档,它应该是这样的:
implicit val credsReader: Reads[JenkinsCredentials] = (
(JsPath).read[String] and
(JsPath \ "description").read[String]
)(JenkinsCredentials.apply _)
与(Json.parse(content) \ "credentials").validate[Seq[JenkinsCredentials]
但是文档没有讨论任何关于将对象的名称提取为在其他地方使用的字段的内容...
编辑:澄清
我的最终状态是从 json 对象而非数组解析的 Seq
of JenkinsCredentials
。由于 JSON 的结构。对于 [=37= 下的每个潜在凭证条目,我会将 "credentials": "UUID" 和 "credentials":"UUID":"description" 拉出到一个对象中]
我没有使用 Reads[T]
方法解决了这个问题:
//Get the key names inside of the credentials
(json \ "credentials").as[JsObject].fields.map{ case (credId, value) =>
JenkinsCredentials(credId, (value \ "description").validate[String].get)
}
这有效,但不验证位,也不使用转换器。