Scala中使用fastJson 解析json字符串

时间:2023-03-08 16:47:18

添加依赖

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

2.解析json字符

2.1可以通过JSON中的parseObject方法,把json字符转转换为一个JSONObject对象

 val jsonOBJ :JSONObject  = JSON.parseObject(json串)

2.2然后可调用JSONObject中的方法,根据key获取值

//获取这种类型的 {"dataId":123,"dataType":"redis"}可以使用getString
val getStr : String = jsonOBJ.getString("dataId")

2.3对于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 方法获取每一个字段

2.4如果想要遍历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尽心循环

3.实例

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},]}

Scala中使用fastJson 解析json字符串

解析这一条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)
})
}
}