在解析JSON对象时遇到问题 - JField(xyz,JDouble(90.21))并访问值“xyz”

时间:2014-09-11 07:49:15

标签: json scala lift-json

我使用Lift库中的parse()函数解析了HTTP API的响应

val resultObj = parse(response)
val dps = resultObj \\ "dps"
println("dps are "+dps)

我现在有这样的事情:

JObject(List(JField(1410418778,JDouble(0.0)), JField(1410418947,JDouble(0.0)),       JField(1410419163,JDouble(0.0)), JField(1410419314,JDouble(0.0)))

我想要检索“1410418778”和相应的双重值,即0.0。

我尝试了以下内容:

dps.children.foreach(element=>{
  println("element "+element+ "and its extract is Double "+
    element.extract[Double]+" and its String extract is "+
    element.extract[String])
  val child = element.children
  println("element child "+child)
})

输出::

element JField(1410420437,JDouble(1.0))and its extract is Double 1.0 and its String extract is 1.0
element child List(JDouble(1.0))

无论是提取[String]还是提取[Double],都只给出JDouble()字段中的值。我也可以从中提取字符串时间戳吗? 提前谢谢!

1 个答案:

答案 0 :(得分:0)

不必要地使答案复杂化。它就像这样直接:

val resultMap : Map[String , Any] = dps.asInstanceOf[JObject].values
    println(resultMap)

来自这里:

Extract Json values as Map with lift-json