Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to parse Json format in Scala

2025-01-19 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >

Share

Shulou(Shulou.com)06/02 Report--

How to parse Json format in Scala? I believe many inexperienced people don't know what to do about it. Therefore, this article summarizes the causes and solutions of the problem. Through this article, I hope you can solve this problem.

Import scala.util.parsing.json._ / / use scala native package def main (args: Array [String]): Unit = {def regJson (json: option [any]) = json match {case Some (map: Map [String, Any]) = > map// case None = > "erro" / / case other = > "Unknow data structure:" + other} val str = "{\" host\ ":\" td_test\ ",\" ts\ ": 1486979192345 \ "device\": {\ "tid\":\ "a123456\",\ "os\":\ "android\",\ "sdk\":\ "1.0.3\"} \ "time\": 1501469230058} "val jsonS = JSON.parseFull (str) val first = regJson (jsonS) / / get level 1 key println (first.get (" host ")) / / get level 2 key val dev = first.get (" device ") println (dev) val sec = regJson (dev) println (sec.get (" tid ") .toString.replace (" Some (","). Replace (")" "")} / / run result Some (td_test) Some (Map (tid-> a123456, os-> android, sdk-> 1.0.3)) a123456import com.google.gson. {JsonObject, JsonParser} / / use Gson package def main (args: Array [String]): Unit = {gson ("{\" host\ ":\" td_test\ ",\" ts\ ": 1486979192345,\" device\ ": {\" tid\ ":\" a123456\ " \ "os\":\ "android\",\ "sdk\":\ "1.0.3\"},\ "time\": 1501469230058} ")} def gson (str: String) = {val json = new JsonParser () val obj = json.parse (str) .asInstanceOf [JsonObject] println (obj.get (" device ")) println (obj.get (" ts "))} / / result: {" tid ":" a123456 "," os ":" android " "sdk": "1.0.3"} 1486979192345 finish reading the above content Have you mastered the method of parsing Json format in Scala? If you want to learn more skills or want to know more about it, you are welcome to follow the industry information channel, thank you for reading!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Internet Technology

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report