Scala中使⽤fastJson解析json字符串
Scala中使⽤fastJson 解析json字符串
添加依赖
<!--解析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 = String("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= JSONArray("resultData")
//获取result中的的数组的对应的第⼀个JSONObject
val nObject: JSONObject = JSONObject(0)
//或取⾥⾯的value值
val str = String("binlog")
//⾥⾯的column对应的还是⼀个数组类型的当然还可以使⽤getJSONArray
val column : JSONArray = JSONArray("column")
//可以通过上⾯的getString ⽅法获取每⼀个字段
2.4如果想要遍历JSONArray中的所有数据,想不使⽤getJSONObject⽅法,但是想要这⾥⾯的
遍历的所有的JSONObject可以使⽤
llection.JavaConversions._
//可以把Java中的集合转成Scala中的集合
//先把JSONArray转换成迭代器Iterator[AnyRef]类型,再转换为List
转换为List时需要导⼊上⾯的包
val list: List[AnyRef] = result.iterator().toList
val listOBJ: List[JSONObject] = list.map(m=>
JSON.String)
/*或者使⽤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},]}
解析这⼀条Json字符串:
fastjson怎么用import com.alibaba.fastjson.{JSON, JSONArray, JSONObject}
llection.immutable
import scala.io.Source
object JsonDemo02 {
def main(args: Array[String]): Unit = {
val lines: Iterator[String] = Source.fromFile("D:\\").getLines()
val list: List[String] = List
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中的数据可以使⽤
llection.JavaConversions._ //可以把Java中的集合转成Scala中的集合
val list: List[AnyRef] = oNArray.iterator().toList
val listOBJ: List[JSONObject] = list.map(m=> JSON.String)/*或者使⽤m.asInstanceOf[JSONObject]*/)
val str1 = String(0)
//也可以通过getJSONObject(下标) 获取相应的JSONObject
val nObject: JSONObject = JSONObject(1)
//获取column
val value = String("column")
val array = JSONArray("column")
println(str1)
println(value)
String(0))
val on1 = JSONObject(0)
val str3 = String("modified")
println(str3)
})
}
}
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。
发表评论