scala的json



val str1="{\"id\":\"001\",\"value\":[{\"fruitName\":\"apple\",\"weight\":\"1\"},{\"fruitName\":\"orange\",\"weight\":\"2\"},{\"fruitName\":\"strawberry\",\"weight\":\"3\"}]}"

val jsonobj=new JSONObject(str1)
println("jsonobj: "+jsonobj)

val strValue=jsonobj.getJSONArray("value")
println("strValue: "+strValue)

for( i <- 0 until strValue.length){
val valueJsonObject=strValue.getJSONObject(i)
val strValue2=valueJsonObject.getString("fruitName")
println("strValue2: "+strValue2)
}

def decodeJSONArray(jsonString: String): Array[String] = {
val ja = new JSONArray(jsonString)
new Array[Int](ja.length).indices.map(i => ja.get(i).toString).toArray
}

val str3=decodeJSONArray(strValue.toString)
println("str3: "+str3.toBuffer)
发布了53 篇原创文章 · 获赞 40 · 访问量 4万+

猜你喜欢

转载自blog.csdn.net/u012761191/article/details/105311320
今日推荐