Scala/Play: parse JSON into Map instead of JsObject

前端 未结 5 1110
小蘑菇
小蘑菇 2020-12-24 14:04

On Play Framework\'s homepage they claim that \"JSON is a first class citizen\". I have yet to see the proof of that.

In my project I\'m dealing with some pretty co

5条回答
  •  醉梦人生
    2020-12-24 14:25

    For further reference and in the spirit of simplicity, you can always go for:

    Json.parse(jsonString).as[Map[String, JsValue]]
    

    However, this will throw an exception for JSON strings not corresponding to the format (but I assume that goes for the Jackson approach as well). The JsValue can now be processed further like:

    jsValueWhichBetterBeAList.as[List[JsValue]]
    

    I hope the difference between handling Objects and JsValues is not an issue for you (only because you were complaining about JsValues being proprietary). Obviously, this is a bit like dynamic programming in a typed language, which usually isn't the way to go (Travis' answer is usually the way to go), but sometimes that's nice to have I guess.

提交回复
热议问题