开发者

Using pattern matching with result of net.liftweb.util.JSONParser.parse

I try to parse a JSON string with the net.liftweb.util.JSONParser. It's parse() method returns a Box[Any] value which I want to process via pattern matching like this:

JSONParser.parse("{foo: 'xxx', bar: 'yyy'}") match {
        case Full(Map) => println("ok")
        case x => println(x)
}

I'd expect this code to pri开发者_运维百科nt "ok" but instead it prints

 Full(Map(foo -> xxx, bar -> yyy))

Does anyone have an idea what's wrong with my statement?


Full(Map) means a Full containing the singleton object Map. If you want Full with something inside that is a Map (the object Map is not one), it should be Full(m: Map) (you can use _ instead of m if you don't need access to that map)


Updated:

scala> import net.liftweb.util._
import net.liftweb.util._

scala> import net.liftweb.common.Full
import net.liftweb.common.Full

scala> JSONParser.parse("{foo: 'xxx', bar: 'yyy'}") match {
 | case Full(m: Map[_, _]) => println("ok")
 | case x => println(x)
 | }
ok

scala> 

(I had forgotten the Full() around the Map[,]

I don't know your requirements, but Lift also has a another json library which is more powerful than JSONParser, it is called lift-json

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜