在Scala中使⽤fastJson解析json字符串
⼀、阿⾥巴巴FastJson是⼀个Json处理⼯具包,包括“序列化”和“反序列化”两部分,它具备如下特征:
速度最快,测试表明,fastjson具有极快的性能,超越任其他的Java Json parser。包括⾃称最快的JackJson;
功能强⼤,完全⽀持Java Bean、集合、Map、⽇期、Enum,⽀持范型,⽀持⾃省;⽆依赖;
⼆、
在Scala中使⽤ 也可使⽤fastJson 解析json字符串,对于json字符串特别复杂的通过fastJSON可以很好的进⾏解析
三、具体使⽤
可以通过JSON中的parseObject⽅法,把json字符转转换为⼀个JSONObject对象
fastjson怎么用val jsonOBJ :JSONObject  = JSON.parseObject(json串)
然后可调⽤JSONObject中的⽅法,根据key获取值
//获取这种类型的 {"dataId":123,"dataType":"redis"}可以使⽤getString
val getStr : String = String("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=  JSONArray("resultData")
//获取result中的 的数组的对应的第⼀个JSONObject
val nObject: JSONObject = JSONObject(0)
//或取⾥⾯的value值
val str = String("binlog")
//⾥⾯的column对应的还是⼀个数组类型的当然还可以使⽤getJSONArray
val column  : JSONArray  = JSONArray("column")
//可以通过上⾯的getString ⽅法获取每⼀个字段
如果想要遍历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尽⼼循环
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","eventT {"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","eventT ⼤致的数据结构
解析这⼀条Json字符串
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小时内删除。