簡體   English   中英

Scala JSON:使用lift收集和解析數組數組

[英]Scala JSON: Collect and parse an array of arrays with lift

我有一個具有數組數組的JSON。 像這樣:

{
  "id": "532242",
  "text": "Some text here. And Here",
  "analysis": {
    "exec": "true",
    "rowID": "always",
    "sentences": {
      "next": null,
      "data": [{
          "sequence": "1",
          "readability_score_lexical": null,
          "readability_score_syntax": null,
          "tokens": [{
              "word": "Some",
              "lemma": "Some"

            },
            {
              "word": "text",
              "lemma": "text"

            }
          ]
        },


        {
          "sequence": "3",
          "readability_score_lexical": null,
          "readability_score_syntax": null,
          "tokens": [{
              "word": "and",
              "lemma": "And"

            },
            {
              "word": "here",
              "lemma": "here"

            }
          ]
        }
      ]
    }
  }
}

結構非常復雜,但是我不能在這方面做任何事情,因為這是API的響應。

我需要的是獲取"tokens"對象列表。

我用lift-web-json做到了這一點:

case class Token(word:String, lemma:String)
implicit val formats: Formats = net.liftweb.json.DefaultFormats
val jsonObj = net.liftweb.json.parse(json)
val tokens = (jsonObj \\ "tokens").children
for (el <- tokens) {
        val m = el.extract[Token]
        println(s"Word ${m.word} and ${m.lemma}")
    }

但它說:

net.liftweb.json.MappingException: No usable value for word
Do not know how to convert JArray(List(JField(word,JString(Some)), JField(word,JString(text))))
[...]
Caused by: net.liftweb.json.MappingException: Do not know how to convert JArray(List(JField(word,JString(Some)), JField(word,JString(text)))) into class java.lang.String

而且我不明白該怎么做。

您應該通過替換以下內容來獲得期望的結果:

val tokens = (jsonObj \\ "tokens").children
for (el <- tokens) {
  val m = el.extract[Token]
  println(s"Word ${m.word} and ${m.lemma}")
}

與:

val tokens = for {
  // tokenList are:
  // JArray(List(JObject(List(JField(word,JString(Some)), JField(lemma,JString(Some)))), JObject(List(JField(word,JString(text)), JField(lemma,JString(text))))))
  // JArray(List(JObject(List(JField(word,JString(and)), JField(lemma,JString(And)))), JObject(List(JField(word,JString(here)), JField(lemma,JString(here))))))
  tokenList <- (jsonObj \\ "tokens").children
  // subTokenList are:
  // List(JObject(List(JField(word,JString(Some)), JField(lemma,JString(Some)))), JObject(List(JField(word,JString(text)), JField(lemma,JString(text)))))
  // List(JObject(List(JField(word,JString(and)), JField(lemma,JString(And)))), JObject(List(JField(word,JString(here)), JField(lemma,JString(here)))))
  JArray(subTokenList) <- tokenList
  // liftToken are:
  // JObject(List(JField(word,JString(Some)), JField(lemma,JString(Some))))
  // JObject(List(JField(word,JString(text)), JField(lemma,JString(text))))
  // JObject(List(JField(word,JString(and)), JField(lemma,JString(And))))
  // JObject(List(JField(word,JString(here)), JField(lemma,JString(here))))
  liftToken <- subTokenList
  // token are:
  // Token(Some,Some)
  // Token(text,text)
  // Token(and,And)
  // Token(here,here)
  token = liftToken.extract[Token]

} yield token

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM