本文主要介绍scala解析json字符串的实例详解的相关资料,希望通过本文能帮助到大家,让大家学习理解这部分内容,需要的朋友可以参考下,希望能帮助到大家。
Scala解析Json字符串的实例详解
1. 添加相应依赖
Json解析工具使用的 json-smart,曾经对比过Java的fastjson、gson。Scala的json4s、lift-json。其中 json-smart 解析速度是最快的。
Easily find JSON paths within JSON objects using our intuitive Json Path Finder
30
<dependency>
<groupId>net.minidev</groupId>
<artifactId>json-smart</artifactId>
<version>2.3</version>
</dependency>2. Scala代码
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)
}
}
}以上就是Scala是如何解析Json字符串的的详细内容,更多请关注php中文网其它相关文章!
每个人都需要一台速度更快、更稳定的 PC。随着时间的推移,垃圾文件、旧注册表数据和不必要的后台进程会占用资源并降低性能。幸运的是,许多工具可以让 Windows 保持平稳运行。
Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号