在Scala中使用fastJson 解析json字符串

一、阿里巴巴FastJson是一个Json处理工具包,包括“序列化”和“反序列化”两部分,它具备如下特征:
速度最快,测试表明,fastjson具有极快的性能,超越任其他的Java Json parser。包括自称最快的JackJson;
功能强大,完全支持Java Bean、集合、Map、日期、Enum,支持范型,支持自省;无依赖;
二、
在Scala中使用 也可使用fastJson 解析json字符串,对于json字符串特别复杂的通过fastJSON可以很好的进行解析
三、具体使用

可以通过JSON中的parseObject方法,把json字符转转换为一个JSONObject对象
 val jsonOBJ :JSONObject  = JSON.parseObject(json串)

然后可调用JSONObject中的方法,根据key获取值
//获取这种类型的 {"dataId":123,"dataType":"redis"}可以使用getString
val getStr : String = jsonOBJ.getString("dataId")

对于JSON中的套JSON字符串的可以使用
{
    "dataId":123,
    "dataType":"mysql",
    "resultData":[
                  {"binlog":"mysql_binlog.000","column":[{"name":"single","type":"int(5)"},{"name":"single3","type":"int(5)"} ]},
                  {"binlog1":"redis_binlog.000","column":[{"name":"single3","type":"int(5)"},{"name":"single3","type":"int(5)"} ]},
                 ]
}

//解析稍微复杂类型的可以使用,上面实际上是一条json,为了更好的看清结构所以换了个行

val  result : JSONArray=   jsonOBJ.getJSONArray("resultData")
//获取result中的 的数组的对应的第一个JSONObject
val nObject: JSONObject = result.getJSONObject(0)
//或取里面的value值
val str = nObject.getString("binlog")
//里面的column对应的还是一个数组类型的当然还可以使用getJSONArray
val column  : JSONArray   = nObject.getJSONArray("column")
//可以通过上面的getString 方法获取每一个字段
 

如果想要遍历JSONArray中的所有数据,想不使用getJSONObject方法,但是想要
这里面的遍历的所有的JSONObject可以使用

      import scala.collection.JavaConversions._  
      //可以把Java中的集合转成Scala中的集合
      //先把JSONArray转换成迭代器Iterator[AnyRef]类型,再转换为List
      转换为List时需要导入 上面的包
      
      val list: List[AnyRef] = result.iterator().toList
      
      val listOBJ: List[JSONObject] = list.map(m=> 
      JSON.parseObject(m.toString)
      /*或者使用m.asInstanceOf[JSONObject]*/
      )
      然后可以使用for循环或者foreach尽心循环

1、使用首先在maven依赖中加入一下依赖

 <!--解析json字符串-->
        <dependency>
            <groupId>com.alibaba</groupId>
            <artifactId>fastjson</artifactId>
            <version>1.2.36</version>
        </dependency>

这是一条超长的json字符串

{"dataId":123,"dataType":"redis","resultData":[{"binlog":"mysql_binlog.000","column":[{"columnname":"single_cloum0","columntype":"varchar(10)","index":0,"modified":false,"pk":false,"sqlType":0,"value":"7"},{"columnname":"single_cloum1","columntype":"varchar(10)","index":1,"modified":false,"pk":false,"sqlType":0,"value":"7"},{"columnname":"single_cloum2","columntype":"int(5)","index":2,"modified":false,"pk":false,"sqlType":0,"value":"7"},{"columnname":"single_cloum3","columntype":"int(5)","index":3,"modified":false,"pk":false,"sqlType":0,"value":"7"}],"db":"demo","eventType":"insert","pkValue":"7","sql":"woshisql","table":"student","time":80146942099474},{"binlog":"mysql_binlog.001","column":[{"columnname":"single_cloum0","columntype":"varchar(10)","index":0,"modified":false,"pk":false,"sqlType":0,"value":"9"},{"columnname":"single_cloum1","columntype":"varchar(10)","index":1,"modified":false,"pk":false,"sqlType":0,"value":"9"},{"columnname":"single_cloum2","columntype":"int(5)","index":2,"modified":false,"pk":false,"sqlType":0,"value":"9"},{"columnname":"single_cloum3","columntype":"int(5)","index":3,"modified":false,"pk":false,"sqlType":0,"value":"9"}],"db":"demo","eventType":"insert","pkValue":"9","sql":"woshisql","table":"student","time":80146943574276},]}
大致的数据结构

在这里插入图片描述

 

解析这一条Json字符串

import com.alibaba.fastjson.{JSON, JSONArray, JSONObject}


import scala.collection.immutable
import scala.io.Source

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

    val lines: Iterator[String] = Source.fromFile("D:\\json2.txt").getLines()
    val list: List[String] = lines.toList
    val jSONObjects: immutable.Seq[JSONObject] = list.map(x => {  //取出每一条数据,把数据转换成JSONObject类型
      println(x)
      JSON.parseObject(x)
    })
    jSONObjects.foreach(t=>{
      val str: String = t.getString("resultData")  //取出resultData的数据,
      val oNArray: JSONArray = t.getJSONArray("resultData")
      //result对应的数据是一个 数组中 存的是 [{json字符串},{json字符串}]

      // /想要遍历JSONArray中的数据可以使用
      import scala.collection.JavaConversions._  //可以把Java中的集合转成Scala中的集合
      val list: List[AnyRef] = oNArray.iterator().toList
      val listOBJ: List[JSONObject] = list.map(m=> JSON.parseObject(m.toString)/*或者使用m.asInstanceOf[JSONObject]*/)

      val str1 = oNArray.getString(0)
      //也可以通过getJSONObject(下标)  获取相应的JSONObject
      val nObject: JSONObject = oNArray.getJSONObject(1)
      //获取column
      val value = nObject.getString("column")
      val array = nObject.getJSONArray("column")
      println(str1)
      println(value)
      println(array.getString(0))
      val on1 = array.getJSONObject(0)
      val str3 = on1.getString("modified")
      println(str3)
    })


  }

}

--------------------- 
作者:Lu_Xiao_Yue 
来源:CSDN 
原文:https://blog.csdn.net/Lu_Xiao_Yue/article/details/84146755 
版权声明:本文为博主原创文章,转载请附上博文链接!

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值