欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

Scala解析Json字符串的實(shí)例詳解

 更新時間:2017年10月11日 14:14:04   作者:JeemyJohn  
這篇文章主要介紹了 Scala解析Json字符串的實(shí)例詳解的相關(guān)資料,希望通過本文能幫助到大家,讓大家學(xué)習(xí)理解這部分內(nèi)容,需要的朋友可以參考下

Scala解析Json字符串的實(shí)例詳解

1. 添加相應(yīng)依賴

       Json解析工具使用的 json-smart,曾經(jīng)對比過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)
  }

 }
}

如有疑問請留言或者到本站社區(qū)交流討論,感謝閱讀,希望能幫助到大家,謝謝大家對本站的支持!

相關(guān)文章

最新評論