例如,我在Scala中有这个Map值:
val m = Map(
"name" -> "john doe",
"age" -> 18,
"hasChild" -> true,
"childs" -> List(
Map("name" -> "dorothy", "age" -> 5, "hasChild" -> false),
Map("name" -> "bill", "age" -> 8, "hasChild" -> false)
)
)
{"我想将其转换为JSON字符串表示形式:"}{
"name": "john doe",
"age": 18,
"hasChild": true,
"childs": [
{
"name": "dorothy",
"age": 5,
"hasChild": false
},
{
"name": "bill",
"age": 8,
"hasChild": false
}
]
}
我目前正在使用Play框架v2.3进行开发,但解决方案不需要使用Play JSON库,如果有人能提供Play和非Play的解决方案会很好。
这是我到目前为止没有成功的尝试:
// using jackson library
val mapper = new ObjectMapper()
val res = mapper.writeValueAsString(m)
println(res)
结果:
{"empty":false,"traversableAgain":true}
我不明白为什么我得到了那个结果。
write(m)
,那么会得到not found
的编译错误。 - nullNoTypeHints
,而我在使用DefaultFormats
。 - null