对于具有相同模式的数据的相同查询,我希望json \\ "something"
始终返回相同类型的对象(或者至少是始终同构的对象),但是,请考虑:
val json1 = ("people" -> List(
("person" -> ("name" -> "Joe")),
("person" -> ("name" -> "Marilyn"))))
val json2 = ("people" -> List(
("person" -> ("name" -> "Joe"))))
val json3 = ("people" -> List[(String, String)]())
println((json1 \\ "name")) // JObject(List(JField(name,JString(Joe)), JField(name,JString(Marilyn))))
println((json2 \\ "name")) // JString(Joe)
println((json3 \\ "name")) // JObject(List())
// which causes the following construction to sometimes fail
println((json1 \\ "name").children map { case JField(_, JString(name)) => name })
// List(Joe, Marilyn)
println((json2 \\ "name").children map { case JField(_, JString(name)) => name })
// List() !!!!!
println((json3 \\ "name").children map { case JField(_, JString(name)) => name })
// List()
...因此 n = 0 和 n >= 2 的情况得到一致处理,但 n = 1 的特殊情况是JValue
.
为什么会这样?是设计使然吗?
与在 a 上的映射进行比较List
val people = List(Person(name = "Joe"), Person(name = "Mary"))
people.map(_.name) # => returns a List
val people = List(Person(name = "Joe"))
people.map(_.name) # => returns a List
val people = List()
people.map(_.name) # => returns a List
与 Scala 的 XML 比较
val xml1 = <people> <person><name>Joe</name></person> <person><name>Marylin</name></person> <person><name>Erik</name></person> </people>
val xml2 = <people> <person><name>Erik</name></person> </people>
val xml3 = <people> </people>
Seq(xml1, xml2, xml3).map(_ \\ "name") foreach (x => println(s"${x.getClass}\t${x.length}\t$x"))
// OUTPUT:
// class scala.xml.NodeSeq$$anon$1 3 <name>Joe</name><name>Marylin</name><name>Erik</name>
// class scala.xml.NodeSeq$$anon$1 1 <name>Erik</name>
// class scala.xml.NodeSeq$$anon$1 0
那么为什么不应该期望 lift-json\\
运算符具有相同的语义呢?
http://liftweb.net/api/26/api/#net.liftweb.json.package上的文档有:
按名称查询JSON 字段的类似 XPath 的表达式。返回所有匹配的字段。