json4s错误:值\\不是(String,org.json4s.JsonAST.JObject)的成员

时间:2016-01-22 01:32:40

标签: scala json4s

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

Json4s Readme中有以下代码:

chris$ sudo pecl install oauth
pecl/oauth requires PHP (version >= 7.0.0), installed version is 5.5.29
No valid packages found
install failed

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

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" ) 下运行示例。有什么想法吗?

1 个答案:

答案 0 :(得分:1)

如果您稍微修改json声明,请执行以下操作:

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

......它会起作用。

我认为编译器会假定您使用StringJObject声明一个元组,而您实际上想要声明一个完整的JObject