import 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 <- 0 until jsonArrGet.toArray.length){
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 <- listOBJ){
println("ele: "+ele)
val str=ele.getString("fruitName")
println("ele.getStr: "+str)
}