您可以使用您最喜欢的 JSON 库来创建字符串。下面是一个json4s的例子:
scala> import org.json4s.jackson.Serialization
import org.json4s.jackson.Serialization
scala> import org.json4s.NoTypeHints
import org.json4s.NoTypeHints
scala> implicit val formats = Serialization.formats(NoTypeHints)
formats: org.json4s.Formats{val dateFormat: org.json4s.DateFormat; val typeHints: org.json4s.TypeHints} = org.json4s.Serialization$$anon$1@7c206b14
scala> val offsets = Map("topic1" -> Map("0" -> 1, "1" -> -1, "2" -> -2), "topic2" -> Map("0" -> 0, "1" -> -1))
offsets: scala.collection.immutable.Map[String,scala.collection.immutable.Map[String,Int]] = Map(topic1 -> Map(0 -> 1, 1 -> -1, 2 -> -2), topic2 -> Map(0 -> 0, 1 -> -1))
scala> Serialization.write(offsets)
res0: String = {"topic1":{"0":1,"1":-1,"2":-2},"topic2":{"0":0,"1":-1}}