如何将play jsObject转换为JSONObject?
例如:
val samplePlayJson = Json.obj(
"name" -> "Watership Down",
"location" -> Json.obj("lat" -> 51.235685, "long" -> -1.309197),
"residents" -> Json.arr(
Json.obj(
"name" -> "Fiver",
"age" -> 4,
"role" -> JsNull
),
Json.obj(
"name" -> "Bigwig",
"age" -> 6,
"role" -> "Owsla"
)
)
)
当我厌倦了将samplePlayJson
传递给接受JSONObject的Java api时,我遇到了类型不匹配错误
type mismatch;
found : play.api.libs.json.JsObject
required: org.json.JSONObject
如何将上面的samplePlayJson转换为org.json.JSONObject
?
你可以将你的play.api.libs.json.JsObject
转换成字符串并使org.json.JSONObject
从中变换。我没有找到任何直接的方法。
new JSONObject(samplePlayJson.toString())