Scala解析Json字符串的实例详解
Scala解析Json字符串的实例详解
1. 添加相应依赖
Json解析工具使用的 json-smart,曾经对比过Java的fastjson、gson。Scala的json4s、lift-json。其中 json-smart 解析速度是最快的。
<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) } } }
如有疑问请留言或者到本站社区交流讨论,感谢阅读,希望能帮助到大家,谢谢大家对本站的支持!
相关推荐
匆匆那些年 2020-10-15
TheBigBlue 2020-07-28
shenwenjie 2020-07-07
muhongdi 2020-07-07
waitwolf 2020-07-08
yunfenglee 2020-07-08
yunfenglee 2020-07-08
kekeromer 2020-07-08
匆匆那些年 2020-07-07
liqinglin0 2020-07-05
TheBigBlue 2020-07-05
kekeromer 2020-06-13
zhixingheyitian 2020-06-08
TheBigBlue 2020-06-06
liqinglin0 2020-06-01
liqinglin0 2020-06-01
yunfenglee 2020-05-30
MICKEYSTUDY 2020-05-28
muhongdi 2020-05-19