SparkCore解析json数据fastjson

import com.alibaba.fastjson.JSON
import org.apache.spark.{SparkConf, SparkContext}
import org.apache.spark.rdd.RDD


object simple {
	def main(args: Array[String]) {
  		val conf = new SparkConf().setAppName("WordCount").setMaster("local")
 		val sc = new SparkContext(conf)
  		val file = sc.textFile("E:\\aa.json")
	  	file.foreach { myrecord =>//注意map有返回值,但是foreach没有返回值
    		val json = JSON.parseObject(myrecord)
    	       //获取成员
   		 val fet = json.get("name")
   		 println(fet)
   		 //println(vtm)
   		 //返回多级成员
   		 val client = json.getJSONObject("goodsproperties").get("phoneNumber")
   		 println(client)
 		 }
 	 sc.stop()
	}
 
  
}

 
  

Json:scala解析json字符串,获取多级key(2)
import com.alibaba.fastjson.JSON

object Test{
  def main(args: Array[String]): Unit = {

    val json = "{\"user_details\":{\"data_column\":\"suuid\"},\"fliter\":{\"event\":\"where event='PageView' or event='ExEvent' or event='Consumption'\"}}"

    //获取一级key,以及值
    println(JSON.parseObject(JSON.parseObject(json).get("user_details").toString))

    //获取二级key,以及值
    println(JSON.parseObject(JSON.parseObject(json).get("user_details").toString).get("data_column").toString)

    //获取一级key,以及值
    println(JSON.parseObject(JSON.parseObject(json).get("fliter").toString))

    //获取二级key,以及值
    println(JSON.parseObject(JSON.parseObject(json).get("fliter").toString).get("event").toString)
  }
}
运行结果:
{"data_column":"suuid"}
suuid
{"event":"where event='PageView' or event='ExEvent' or event='Consumption'"}
where event='PageView' or event='ExEvent' or event='Consumption'

你可能感兴趣的:(SparkCore解析json数据fastjson)