More on that Scala object….

I changed my Scala object a bit, to break out the items in the map it was getting.  What it was doing was going to an API, getting some data, and then getting more data, discarding any errors.  However, the data it was getting was coming back as a map.

Since JSON has both integers and strings in it, it returned as Map Type Any.

I couldn’t understand why I just couldn’t print out the values from the map.  But I would get an error saying that I couldn’t apply println or other methods to Type Any.

What it’s saying is, that I need to transform the Type from Any to String.

import scala.util.parsing.json._
object Parser {
  def main(args:Array[String]){
    for (i <- 1 until 1000) {
      try {
        val json = JSON.parseFull(“” + i).getLines().mkString(“\n”))
        val jsonType = json.asInstanceOf[Option[Map[String, String]]]
        val jsonTitle =“title”))
        val jsonLoc =“category”))
        val jsonComplete = jsonTitle ++ jsonLoc

        jsonComplete foreach {println}
      }catch {
        case e: Exception =>

I did it by doing the above in Red.  I thought that the mkString appended to the val json would have done this, but it didn’t.  In fact I had to make a new variable (val jsonType) that takes the json variable and does a asInstanceOf[Option[Map[String, String]]  This was how it worked for the JSON I was getting back.

Then to pull out a element from the map, I’m using a variable to“title”)) or whatever value I want.

My current frustration though, is that by doing it this way, I have to combine the maps to get all the variables I want to print out.  Like if I want: title, category to print line by line, it’s tough. I add the two maps together with the ++ operator and then print out that new variable.  But it prints like this:

It’s not the formatting I want. ugh!  So that’s my next hurdle.


No responses yet

Leave a Reply

Your email address will not be published. Required fields are marked *