繁体   English   中英

如何在Scala上解析Json

[英]How to Parse Json on Scala

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

在这里,我想要“ l”值,但我无法获取它,当我使用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

并尝试了此链接,但我无法获得它,我在这里做什么?

使用JSON.parseFull解析将返回任意选项。

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

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

或键入强制类型转换(不推荐)

好吧...问题是您拥有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中推荐的处理方式,但是可以帮助您从基本的角度理解您应该做什么。 不断学习...,您将逐渐了解处理事物的更好方法。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM