ika memo

Pocket

scala

とてもよくないので随時更新
よくなっていくはず

https://gist.github.com/astail/0474223cc020a35b4f0edd1d085a2311

scala> import scala.io.Source
import scala.io.Source

scala> import org.json4s._
import org.json4s._

scala> import org.json4s.jackson.JsonMethods._
import org.json4s.jackson.JsonMethods._

scala>     val api = "https://spla2.yuu26.com/regular/now"
api: String = https://spla2.yuu26.com/regular/now

scala>     val source = Source.fromURL(api, "utf-8")
source: scala.io.BufferedSource = non-empty iterator

scala>     val str = source.getLines.mkString
str: String = {"result":[{"rule":"ナワバリバトル","rule_ex":{"key":"turf_war","name":"ナワバリバトル","statink":""},"maps":["ショッツル鉱山","チョウザメ造船"],"maps_ex":[{"id":17,"name":"ショッツル鉱山","statink":""},{"id":3,"name":"チョウザメ造船","statink":""}],"start":"2018-05-18T17:00:00","start_utc":"2018-05-18T08:00:00+00:00","start_t":1526630400,"end":"2018-05-18T19:00:00","end_utc":"2018-05-18T10:00:00+00:00","end_t":1526637600}]}

scala>     val jsonObj = parse(str)
jsonObj: org.json4s.JValue = JObject(List((result,JArray(List(JObject(List((rule,JString(ナワバリバトル)), (rule_ex,JObject(List((key,JString(turf_war)), (name,JString(ナワバリバトル)), (statink,JString())))), (maps,JArray(List(JString(ショッツル鉱山), JString(チョウザメ造船)))), (maps_ex,JArray(List(JObject(List((id,JInt(17)), (name,JString(ショッツル鉱山)), (statink,JString()))), JObject(List((id,JInt(3)), (name,JString(チョウザメ造船)), (statink,JString())))))), (start,JString(2018-05-18T17:00:00)), (start_utc,JString(2018-05-18T08:00:00+00:00)), (start_t,JInt(1526630400)), (end,JString(2018-05-18T19:00:00)), (end_utc,JString(2018-05-18T10:00:00+00:00)), (end_t,JInt(1526637600)))))))))

scala>     val hogehoge = jsonObj.asInstanceOf[JObject].values
hogehoge: scala.collection.immutable.Map[String,Any] = Map(result -> List(Map(maps_ex -> List(Map(id -> 17, name -> ショッツル鉱山, statink -> ""), Map(id -> 3, name -> チョウザメ造船, statink -> "")), start_t -> 1526630400, rule -> ナワバリバトル, end_t -> 1526637600, end_utc -> 2018-05-18T10:00:00+00:00, end -> 2018-05-18T19:00:00, rule_ex -> Map(key -> turf_war, name -> ナワバリバトル, statink -> ""), start_utc -> 2018-05-18T08:00:00+00:00, start -> 2018-05-18T17:00:00, maps -> List(ショッツル鉱山, チョウザメ造船))))

scala> 

scala>     val a = hogehoge("result")
a: Any = List(Map(maps_ex -> List(Map(id -> 17, name -> ショッツル鉱山, statink -> ""), Map(id -> 3, name -> チョウザメ造船, statink -> "")), start_t -> 1526630400, rule -> ナワバリバトル, end_t -> 1526637600, end_utc -> 2018-05-18T10:00:00+00:00, end -> 2018-05-18T19:00:00, rule_ex -> Map(key -> turf_war, name -> ナワバリバトル, statink -> ""), start_utc -> 2018-05-18T08:00:00+00:00, start -> 2018-05-18T17:00:00, maps -> List(ショッツル鉱山, チョウザメ造船)))

scala>     val b = a.asInstanceOf[List[scala.collection.immutable.Map[String,Any]]]
b: List[scala.collection.immutable.Map[String,Any]] = List(Map(maps_ex -> List(Map(id -> 17, name -> ショッツル鉱山, statink -> ""), Map(id -> 3, name -> チョウザメ造船, statink -> "")), start_t -> 1526630400, rule -> ナワバリバトル, end_t -> 1526637600, end_utc -> 2018-05-18T10:00:00+00:00, end -> 2018-05-18T19:00:00, rule_ex -> Map(key -> turf_war, name -> ナワバリバトル, statink -> ""), start_utc -> 2018-05-18T08:00:00+00:00, start -> 2018-05-18T17:00:00, maps -> List(ショッツル鉱山, チョウザメ造船)))

scala>     val c = b(0)("maps_ex")
c: Any = List(Map(id -> 17, name -> ショッツル鉱山, statink -> ""), Map(id -> 3, name -> チョウザメ造船, statink -> ""))

scala>     val d = c.asInstanceOf[List[scala.collection.immutable.Map[String,Any]]]
d: List[scala.collection.immutable.Map[String,Any]] = List(Map(id -> 17, name -> ショッツル鉱山, statink -> ""), Map(id -> 3, name -> チョウザメ造船, statink -> ""))

scala> 

scala>     for(xx <- d) println(xx("name"))
ショッツル鉱山
チョウザメ造船

scala> 

scala>     source.close

One Reply to “ika memo”

コメントを残す

メールアドレスが公開されることはありません。

このサイトはスパムを低減するために Akismet を使っています。コメントデータの処理方法の詳細はこちらをご覧ください