scala的fastjson
2021-03-28 13:27
标签:遍历 raw col port collect tostring get alibaba fas scala的fastjson 标签:遍历 raw col port collect tostring get alibaba fas 原文地址:https://www.cnblogs.com/ShyPeanut/p/12620975.htmlimport com.alibaba.fastjson.{JSON, JSONObject}
val str="{\"boxId\":\"001\",\"allContent\":[{\"fruitName\":\"apple\",\"weight\":\"1\"},{\"fruitName\":\"orange\",\"weight\":\"2\"},{\"fruitName\":\"strawberry\",\"weight\":\"3\"}]}"
val jsonobj=JSON.parseObject(str)
val result1=jsonobj.getString("allContent")
println(result1)
val jsonArrGet=jsonobj.getJSONArray("allContent")
println(jsonArrGet)
//遍历JSONArray中的所有数据
//getJSONObject + 数组下标
for(i val nObject=jsonArrGet.getJSONObject(i)
println("nObject: "+nObject)
val nStr=nObject.getString("fruitName")
println("nStr: "+nStr)
}
//遍历JSONArray中的所有数据
//JSONArray转化为集合
import scala.collection.JavaConversions._
val list = jsonArrGet.iterator().toList
val listOBJ = list.map(m=> JSON.parseObject(m.toString)) //或者 m.asInstanceOf[JSONObject]
for (ele println("ele: "+ele)
val str=ele.getString("fruitName")
println("ele.getStr: "+str)
}