json4s error: value \\ is not a member of (String, org.json4s.JsonAST.JObject)

json4s error: value \\ is not a member of (String, org.json4s.JsonAST.JObject)

首先,我是 Scala 的新手,所以请原谅我最后的愚蠢错误。

Json4s Readme中有如下代码:

scala> import org.json4s.jackson.JsonMethods._
scala> import org.json4s.JsonDSL._


scala> val json =
  ("person" ->
  ("name" -> "Joe") ~
  ("age" -> 35) ~
    ("spouse" ->
    ("person" ->
     ("name" -> "Marilyn") ~
     ("age" -> 33)
    )
  )
)

scala> json \ "spouse"
res0: org.json4s.JsonAST.JValue = JObject(List(
      (person,JObject(List((name,JString(Marilyn)), (age,JInt(33)))))))

当我 运行 此代码时出现以下错误:

error: value \ is not a member of (String, org.json4s.JsonAST.JObject)
json \ "spouse"
     ^

我的 sbt 文件如下:

name := "Impressions"
version := "1.0"
scalaVersion := "2.10.6"

libraryDependencies ++= Seq(
  "org.apache.spark" %% "spark-core" % "1.4.1" % "provided",
  //"org.json4s"  %% "json4s-native" % "3.3.0"
  "org.json4s"  %% "json4s-jackson" % "3.3.0"
)

和我运行 sbt console下的例子。有什么想法吗?

如果您像这样稍微修改 json 声明:

val json: JObject =
  ("person" ->
  ("name" -> "Joe") ~
  ("age" -> 35) ~
    ("spouse" ->
    ("person" ->
     ("name" -> "Marilyn") ~
     ("age" -> 33)
    )
  )
)

...它会起作用。

我认为编译器假定您正在声明一个带有 StringJObject 的元组,而您实际上想要声明一个完整的 JObject