如何在Scala上解析Json

时间:2016-12-26 14:17:34

标签: json scala parsing

在这里,我附上了我的代码(纯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)

            }
          }
        }
     }

0 \,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)))

在这里,我想要&#34; l&#34;价值,但我无法得到它,当我使用map / foreach时,它返回

$ scalac Stock.scala
Stock.scala:30: error: value map is not a member of Any
                val s5 = s4.map{ ex => ex }
                                            ^
one error found

试过这个link,但我无法得到它,我在做什么?

2 个答案:

答案 0 :(得分:1)

使用JSON.parseFull进行解析会返回任意选项。

scala> JSON.parseFull("""{"key": "value"}""")
res2: Option[Any] = Some(Map(key -> value))

因此,使用模式匹配来提取所需的类型。

或输入(不推荐)

答案 1 :(得分:0)

嗯......问题是你的S4为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")

注意::这不是Scala中推荐的做事方式,但可以帮助您了解在基本级别上应该做什么。继续学习......你会逐渐理解处理事情的更好方法。