我有下一个列表和字符串:
val s = List(JSONObject, JSONObject, JSONObject)
val data = s"""[${s(0)}, ${s(1)}, ${s(2)}]"""
是否可以将列表传递给s
函数,因此我不会按索引获取列表元素?
答案 0 :(得分:1)
您正在寻找mkString
:
List(
JSONObject(Map("hello" -> "world", "stackoverflow" -> "ishere")),
JSONObject(Map("1" -> 1))
).map(_.toString()).mkString(", ")
收率:
scala> :pa
// Entering paste mode (ctrl-D to finish)
List(
JSONObject(Map("hello" -> "world", "stackoverflow" -> "ishere")),
JSONObject(Map("1" -> 1))
).map(_.toString()).mkString(", ")
// Exiting paste mode, now interpreting.
res2: String = {"hello" : "world", "stackoverflow" : "ishere"}, {"1" : 1}
答案 1 :(得分:-1)
试试这个
val s = List("JSONObject", "JSONObject", "JSONObject")
val result = s.mkString("[",",","]")
result: String = [JSONObject,JSONObject,JSONObject]