小白学习Spark系列五:scala解析多级json格式字符串

 一、背景

  处理json格式的字符串,key值一定为String类型,但value不确定是什么类型,也可能嵌套json字符串,以下是使用 JSON.parseFull 来解析多层json。

二、实例代码

import collection.mutable.Map
import scala.util.parsing.json._
def regJson(json:Option[Any]) = json match {
      //转换类型
      case Some(map:collection.immutable.Map[String, Any]) => map
}

def str_json(string_json:String):collection.immutable.Map[String,Any]=
{
    var first :collection.immutable.Map[String, Any] = collection.immutable.Map()
    val jsonS = JSON.parseFull(string_json)
    //不确定数据的类型时,此处加异常判断
    if (jsonS.isInstanceOf[Option[Any]]){
        first = regJson(jsonS)
    }
    first
    }
val jsonStr = """{"id":"1", "name":"jack","detail_info":{"age":"18","sex":"man"}}"""
val parse_str = str_json(jsonStr)
//parse_str: scala.collection.immutable.Map[String,Any] = Map(id -> 1, name -> jack, detail_info -> Map(age -> 18, sex -> man))
val id = parse_str("id")
//res7: Any = 1,此处为Any类型,一定要在使用前转换为原有类型,可以用asInstanceOf函数
val id_str = parse_str("id").toString
//res8: String = 1
val detail_info = parse_str("detail_info")
//res9: Any = Map(age -> 18, sex -> man)
//val parse_str("detail_info").asInstanceOf[Map[String, String]]
//提示错误:java.lang.ClassCastException: scala.collection.immutable.Map$Map2 cannot be cast to scala.collection.mutable.Map。注意Map类型
val parse_detail = parse_str("detail_info").asInstanceOf[scala.collection.immutable.Map[String,Any]]
//parse_detail: scala.collection.immutable.Map[String,Any] = Map(age -> 18, sex -> man)
val sex_str = parse_detail("sex").toString
//res14: String = man
val parse_detail = parse_str("detail_info").asInstanceOf[scala.collection.immutable.Map[String,Any]]

 三、分析

  以上代码中,需要注意的两点是(1)类型转换时,默认的Map为 scala.collection.immutable.Map,由于第一行声明了可变Map类型,所在在后期 asInstanceOf ()嵌套的json格式字符串时,需要显示声明不可变map类型。(2)由于不确定json字符串的值类型,需要使用Any类型预定义,在具体解析某个字段时,必须用 asInstanceOf() 来解析成原有的数据类型,否则会报错。

posted @ 2018-11-19 22:29  白白毛狗  阅读(6260)  评论(0编辑  收藏  举报