[Lift] json extraction into useful list

46 views
Skip to first unread message

Eric Brigham

unread,
Nov 8, 2010, 6:09:50 PM11/8/10
to lif...@googlegroups.com
I guess this isn't a lift specific question, but I'm sure you know how to do this:

Given the following json:
{"asks":[[0.251,300],[0.2587,400],[0.2667,400],[0.414,100]],"bids":[[0.191,1000],[0.1956,400]]}

How do I extract from it so that I'll be left with two separate Lists:
val asks = List( (0.251, 300), ....,(0.414,100) )
val bids = List( (0.191,1000), ...., (0.1956,400) )

Sorry for such the newbie question, I'm still wrapping my head around all the different paradigms available in scala/lift.  Thanks in advance for your help!
--Eric

David Pollak

unread,
Nov 8, 2010, 6:26:17 PM11/8/10
to lif...@googlegroups.com
Welcome to Scala version 2.8.0.final (Java HotSpot(TM) 64-Bit Server VM, Java 1.6.0_22).
Type in expressions to have them evaluated.
Type :help for more information.

scala> val json = """{"asks":[[0.251,300],[0.2587,400],[0.2667,400],[0.414,100]],"bids":[[0.191,1000],[0.1956,400]]}
     | """
json: java.lang.String =
{"asks":[[0.251,300],[0.2587,400],[0.2667,400],[0.414,100]],"bids":[[0.191,1000],[0.1956,400]]}


scala> import net.liftweb.json._
import net.liftweb.json._

scala> import JsonParser.parse
import JsonParser.parse

scala> import JsonAST._
import JsonAST._

scala> val js = parse(json)
js: net.liftweb.json.JsonAST.JValue = JObject(List(JField(asks,JArray(List(JArray(List(JDouble(0.251), JInt(300))), JArray(List(JDouble(0.2587), JInt(400))), JArray(List(JDouble(0.2667), JInt(400))), JArray(List(JDouble(0.414), JInt(100)))))), JField(bids,JArray(List(JArray(List(JDouble(0.191), JInt(1000))), JArray(List(JDouble(0.1956), JInt(400))))))))

scala> for {                             
     | JField("bids", bids) <- js        
     | JField("asks", asks) <- js        
     | } yield asks -> bids     
res2: List[(net.liftweb.json.JsonAST.JValue, net.liftweb.json.JsonAST.JValue)] = List((JArray(List(JArray(List(JDouble(0.251), JInt(300))), JArray(List(JDouble(0.2587), JInt(400))), JArray(List(JDouble(0.2667), JInt(400))), JArray(List(JDouble(0.414), JInt(100))))),JArray(List(JArray(List(JDouble(0.191), JInt(1000))), JArray(List(JDouble(0.1956), JInt(400)))))))

scala> dpp@cobra:~/tmp/chat$


--
You received this message because you are subscribed to the Google Groups "Lift" group.
To post to this group, send email to lif...@googlegroups.com.
To unsubscribe from this group, send email to liftweb+u...@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/liftweb?hl=en.



--
Lift, the simply functional web framework http://liftweb.net
Beginning Scala http://www.apress.com/book/view/1430219890
Follow me: http://twitter.com/dpp
Blog: http://goodstuff.im
Surf the harmonics

Eric Brigham

unread,
Nov 8, 2010, 6:41:07 PM11/8/10
to lif...@googlegroups.com
It's all making sense now!  Thanks so much!

Eric Brigham

unread,
Nov 8, 2010, 6:48:43 PM11/8/10
to lif...@googlegroups.com
But wait, I'm still missing something.  At the end of the day, how am I left with (or can obtain) a non json variable like:
var asks = List( (0.251,300), (0.2587,400), ... )

Following your code, it seems to me like I'm still left with a big mess?  I think there must be some crucial concept that everyone else takes for granted that I am missing.  Ideas?
--Eric

On Mon, Nov 8, 2010 at 4:26 PM, David Pollak <feeder.of...@gmail.com> wrote:

David Pollak

unread,
Nov 8, 2010, 6:52:21 PM11/8/10
to lif...@googlegroups.com
On Mon, Nov 8, 2010 at 3:48 PM, Eric Brigham <ebri...@gmail.com> wrote:
But wait, I'm still missing something.  At the end of the day, how am I left with (or can obtain) a non json variable like:
var asks = List( (0.251,300), (0.2587,400), ... )

I demonstrated the general concept of using extractors to get the fields out of a JObject.  You can use the same technique to extract from a List[JValue] into the specific types you want.
 
Reply all
Reply to author
Forward
0 new messages