Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/scala/19.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Scala 如何提取用于理解的JSON值_Scala_Lift Json - Fatal编程技术网

Scala 如何提取用于理解的JSON值

Scala 如何提取用于理解的JSON值,scala,lift-json,Scala,Lift Json,我想提取JSON值以便于理解 我的代码是: import net.liftweb.json._ val json = parse(""" { "took": 212, "timed_out": false, "_shards": { "total": 5, "successful": 5, "failed": 0 }, "hits": { "total": 4, "

我想提取JSON值以便于理解

我的代码是:

import net.liftweb.json._

val json = parse("""     
{
    "took": 212,
    "timed_out": false,
    "_shards": {
        "total": 5,
        "successful": 5,
        "failed": 0
    },
    "hits": {
        "total": 4,
        "max_score": 0.625,
        "hits": [
            {
                "_index": "siteindex",
                "_type": "posts",
                "_id": "1",
                "_score": 0.625,
                "_source": {
                    "title": "title 1",
                    "content": "content 1"
                },
                "highlight": {
                    "title": [
                        "<b>title</b> 1"
                    ]
                }
            },
            {
                "_index": "siteindex",
                "_type": "posts",
                "_id": "4",
                "_score": 0.19178301,
                "_source": {
                    "title": "title 4",
                    "content": "content 4"
                },
                "highlight": {
                    "title": [
                        "<b>title</b> 4"
                    ]
                }
            },
            {
                "_index": "siteindex",
                "_type": "posts",
                "_id": "2",
                "_score": 0.19178301,
                "_source": {
                    "title": "title 2",
                    "content": "content 2"
                },
                "highlight": {
                    "title": [
                        "<b>title</b> 2"
                    ]
                }
            },
            {
                "_index": "siteindex",
                "_type": "posts",
                "_id": "3",
                "_score": 0.19178301,
                "_source": {
                    "title": "title 3",
                    "content": "content 3"
                },
                "highlight": {
                    "title": [
                        "<b>title</b> 3"
                    ]
                }
            }
        ]
    }
}      
""")
我的“for”是:

直到这里一切都好

但现在我需要这样的东西:

List(Document2(1,<b>title</b> 1,content 1), Document2(4,<b>title</b> 4,content 4), Document2(2,<b>title</b> 2,content 2), Document2(3,<b>title</b> 3,content 3))
在我的名单上

我的“案例类”是:

我试过这个,但不起作用

val ret: List[Document2] = for {
  JObject(child)                          <- json
  JField("_id",         JString(_id))     <- child
  JField("title",       JString(title))   <- child
  JField("content",     JString(content)) <- child
} yield (Document2( _id, title, content  ))
val ret:List[Document2]=for{

JObject(child)这里有一个类似于@flav的方法的答案,但我将为您提供映射到json的结构以及如何获得最终结果。首先,案例类:

case class Document2(_id:String, title:String, content:String)

case class Results(hits:HitsList)
case class HitsList(hits:List[Hit])
case class Hit(_id:String, _source:Source, highlight:Highlight)
case class Source(title:String, content:String)
case class Highlight(title:List[String])
然后,解析和转换它的代码:

implicit val formats = DefaultFormats
val results = json.extract[Results]
val docs2 = results.hits.hits.map{ hit => 
  Document2(hit._id, hit.highlight.title.head, hit._source.content)
}
 "highlight": {
                    "title": [
                        "<b>*</b> *"
                    ]
                }
"_id": "*",
case class Document2(_id:String, title:String, content:String)
val ret: List[Document2] = for {
  JObject(child)                          <- json
  JField("_id",         JString(_id))     <- child
  JField("title",       JString(title))   <- child
  JField("content",     JString(content)) <- child
} yield (Document2( _id, title, content  ))
<console>:23: warning: `withFilter' method does not yet exist on net.liftweb.json.JValue, using `filter' method instead
         JObject(child)                          <- json

ret: List[Document2] = List()
case class Document2(_id:String, title:String, content:String)

case class Results(hits:HitsList)
case class HitsList(hits:List[Hit])
case class Hit(_id:String, _source:Source, highlight:Highlight)
case class Source(title:String, content:String)
case class Highlight(title:List[String])
implicit val formats = DefaultFormats
val results = json.extract[Results]
val docs2 = results.hits.hits.map{ hit => 
  Document2(hit._id, hit.highlight.title.head, hit._source.content)
}