The 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)

 

Published 53 original articles · won praise 40 · views 40000 +

Guess you like

Origin blog.csdn.net/u012761191/article/details/105311320