2016-12-26 76 views
0

在這裏,我重視我的代碼(純斯卡拉)如何解析JSON在斯卡拉

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) 

      } 
      } 
     } 
    } 

Ø\ Y:

(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))) 

在這裏,我想 「l」 的價值,但我可以」牛逼能得到它,當我使用的地圖/的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,但我可以不能夠得到它,在這裏我做什麼?

+0

軟件包'scala.util.parsing.json'已棄用 – cchantep

+0

檢查SO文檔是否可以幫助您:http://stackoverflow.com/documentation/scala/2348/json#t=20161226173836757393 – pedrorijo91

回答

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中推薦的方法,但可以幫助您瞭解基本級別應該做什麼。繼續學習......你會明白更好的方式來處理事情。

0

解析使用JSON.parseFull返回任何選項。

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

因此,使用模式匹配來提取你想要的類型。

或類型轉換(不推薦)