Reputation: 145
Here, I attached my code (pure Scala)
package stock
import scala.io.Source._
import java.util.Scanner
import java.io.File
import scala.util.parsing.json._
object StockDetails {
def main(args : Array[String]){
val filePath = new java.io.File(".").getCanonicalPath
val source = scala.io.Source.fromFile(filePath +"/stock/file.txt")
// file.txt --> contains
// line 1 --> GOOG - 50, MS - 10
// line 2 --> SGI - 100, GOOG - 50, MS - 10
// line 3 --> GOOG - 100, AMZN - 90, MS - 80
for(line <- source.getLines()) {
val txt = line.split(",")
val s1 = txt.map{ss =>
val s2 = ss.split(" - ")(0).trim()
val holder = fromURL("http://finance.google.com/finance/info?client=ig&q="+s2).mkString
println("holder===========",holder)
val s3 = holder.split("//")(1)
println("s3================",s3)
val s4 = JSON.parseFull(s3).get
println("s4==========================",s4)
}
}
}
}
O\P:
(holder==========================,
// [
{
"id": "660479"
,"t" : "MS"
,"e" : "NYSE"
,"l" : "43.06"
,"l_fix" : "43.06"
,"l_cur" : "43.06"
,"s": "0"
,"ltt":"4:02PM EST"
,"lt" : "Dec 23, 4:02PM EST"
,"lt_dts" : "2016-12-23T16:02:12Z"
,"c" : "+0.27"
,"c_fix" : "0.27"
,"cp" : "0.63"
,"cp_fix" : "0.63"
,"ccol" : "chg"
,"pcls_fix" : "42.79"
}
])
(s3================,
[{
"id": "660479"
,"t" : "MS"
,"e" : "NYSE"
,"l" : "43.06"
,"l_fix" : "43.06"
,"l_cur" : "43.06"
,"s": "0"
,"ltt":"4:02PM EST"
,"lt" : "Dec 23, 4:02PM EST"
,"lt_dts" : "2016-12-23T16:02:12Z"
,"c" : "+0.27"
,"c_fix" : "0.27"
,"cp" : "0.63"
,"cp_fix" : "0.63"
,"ccol" : "chg"
,"pcls_fix" : "42.79"
}
])
(s4==========================,
List(Map(
e -> NYSE,
s -> 0,
cp_fix -> 0.63,
l_cur -> 43.06,
ccol -> chg,
t -> MS,
pcls_fix -> 42.79,
id -> 660479,
l -> 43.06,
l_fix -> 43.06,
c_fix -> 0.27,
c -> +0.27,
cp -> 0.63,
lt -> Dec 23, 4:02PM EST,
lt_dts -> 2016-12-23T16:02:12Z,
ltt -> 4:02PM EST)))
Here, I want the " l " value but i can't able to get it, When I used map/ foreach it's returned as
$ scalac Stock.scala
Stock.scala:30: error: value map is not a member of Any
val s5 = s4.map{ ex => ex }
^
one error found
And tried this link but i can't able to get it, Here what i do?
Upvotes: 0
Views: 1372
Reputation: 13985
Well... the thing is that you have S4 as Any
.
// since we know that s4 is supposed to be a List
// we can type-cast it to List[Any]
val s4List = s4.asInstanceOf[List[Any]]
// now we also know that s4 was actually a list of Map's
// and those maps look to be String -> String
// so we can type-cast all the things inside list to Map[String, String]
val s4MapList = s4List.map(m => m.asInstanceOf[Map[String, String]])
// and we also know that s4 is a List of Map's and has length 1
val map = s4MapList(0)
// lets say you wanted id
val idOption = map.get("id")
// but we know that map has a key "id"
// so we can just get that
val id = map("id")
Note :: This is not the recommended way of doing things in Scala, but can help you understand what are you supposed to do on a basic level. Keep learning... and you will come to understand better ways to deal with things.
Upvotes: 0
Reputation: 14825
Parsing using JSON.parseFull
returns option of any.
scala> JSON.parseFull("""{"key": "value"}""")
res2: Option[Any] = Some(Map(key -> value))
So, use pattern matching to extract the types you want.
or type cast (not recommended)
Upvotes: 1