如何将play.api.libs.json.JsValue转换为org.json.JSONObject

时间:2019-04-18 02:57:30

标签: java json scala playframework play-json

如何将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

1 个答案:

答案 0 :(得分:0)

您可以将play.api.libs.json.JsObject转换为字符串,然后将org.json.JSONObject制成字符串。我没有找到直接的方法。

new JSONObject(samplePlayJson.toString())