This article mainly introduces relevant information on detailed examples of Scala parsing Json strings. I hope this article can help everyone learn and understand this part of the content. Friends in need can refer to it. I hope it can help everyone.
Detailed explanation of examples of Scala parsing Json strings
1. Add corresponding dependencies
Use of Json parsing tools json-smart has been compared with Java's fastjson and gson. Scala’s json4s, lift-json. Among them, json-smart has the fastest parsing speed.
<dependency> <groupId>net.minidev</groupId> <artifactId>json-smart</artifactId> <version>2.3</version> </dependency>
2. Scala code
package Test import java.util import net.minidev.json.JSONObject import net.minidev.json.parser.JSONParser import scala.collection.JavaConversions._ import scala.collection.mutable import scala.util.parsing.json.JSON /** * Created by zhanghuayan on 2017/3/30. */ object Test { def main(args: Array[String]): Unit = { val str2 = "{\"name\":\"jeemy\",\"age\":25,\"phone\":\"18810919225\"}" val jsonParser = new JSONParser() val jsonObj: JSONObject = jsonParser.parse(str2).asInstanceOf[JSONObject] val name = jsonObj.get("name").toString println(name) val jsonKey = jsonObj.keySet() val iter = jsonKey.iterator while (iter.hasNext) { val instance = iter.next() val value = jsonObj.get(instance).toString println("key: " + instance + " value:" + value) } } }
The above is the detailed content of How Scala parses Json strings. For more information, please follow other related articles on the PHP Chinese website!