1
字符串转换成JSON我需要帮助JSON字符串解析成使用playJson斯卡拉 - 使用播放JSON
我写了一个格式,但我不知道如何处理嵌套数组斯卡拉类。
凡公文包类是
case class Document(content: String, score: Double, size: Int, path:String)
和格式化
implicit val similarHashFormatter: Format[SimilarHash] = (
((__ \ "hits" \ "hits" \\ "fields")(0) \ "content_hash")(0).format[String] and
(__ \ "hits" \ "hits" \\ "_score").format[Double] and
((__ \ "hits" \ "hits" \\ "fields")(0) \ "ast_size")(0).format[Int] and
((__ \ "hits" \ "hits" \\ "fields")(0) \ "path")(0).format[String]
) (SimilarHash.apply, unlift(SimilarHash.unapply))
这是我的源JSON
{
"hits": {
"hits": [
{
"score": 1.5204661,
"fields": {
"size": [
557645
],
"path": [
"/user/ubuntu/app
],
"content": [
"images"
]
}
},
{
"score": 1.5199462,
"fields": {
"size": [
556835
],
"path": [
"/user/ubuntu/app
],
"content": [
"documents"
]
}
}
]
}
}
任何想法?
什么是您的Scala类的定义是什么样子?大多数情况下,使用case类的Json.format宏来处理更复杂的模型是最简单的。 – josephpconley
@josephpconley我刚刚添加了Document case类,任何示例如何使用Json.format宏? –