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

问题描述 投票:1回答:1

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

java json scala playframework play-json
1个回答
0
投票

你可以将你的play.api.libs.json.JsObject转换成字符串并使org.json.JSONObject从中变换。我没有找到任何直接的方法。

new JSONObject(samplePlayJson.toString())
© www.soinside.com 2019 - 2024. All rights reserved.