我想遍历github上的Scala JSON工具包产生的集合.问题是JsonParser返回"Any"所以我想知道如何避免以下错误:
"值foreach不是Any的成员".
val json = Json.parse(urls)
for(l <- json) {...}
object Json {
def parse(s: String): Any = (new JsonParser).parse(s)
}
Run Code Online (Sandbox Code Playgroud)
您必须进行模式匹配才能遍历从解析器返回的结构.
/*
* (untested)
*/
def printThem(a: Any) {
a match {
case l:List[_] =>
println("List:")
l foreach printThem
case m:Map[_, _] =>
for ( (k,v) <- m ) {
print("%s -> " format k)
printThem(v)
}
case x =>
println(x)
}
val json = Json.parse(urls)
printThem(json)
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
704 次 |
| 最近记录: |