Scala解析JSON

package com.mlamp.test_json

/**

* @Author: DaChun777

* @Date: 2021/12/8  上午10:35

* @Description:

*/

import scala.util.parsing.json._

import com.google.gson.{JsonElement, JsonObject, JsonParser}

object read_json {

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: JsonObject = json.parse(str).asInstanceOf[JsonObject]

val element: JsonElement = obj.get("device")

val element1: JsonElement = element.getAsJsonObject.get("tid")

//s上面的element1得到的带双引号,需要去掉

    val str1:String = element1.toString.replace("\"", "")

println(element)

println(element1)

println(str1)

println(obj.get("ts"))

}

}

你可能感兴趣的:(Scala解析JSON)