简体   繁体   中英

How do I create JSON objects programmatically in playframework 2.1 scala

http://www.playframework.com/documentation/2.1.x/ScalaJson

That document says the idiomatic style of json creation is:

import play.api.libs.json.Json
Json.obj( "key" -> "value )

However this fails to compile as String -> String is not String -> Json.JsValueWrapper

It appears that play provides the code needed for implicit conversions in play.api.libs.json.{DefaultReads, DefaultWrites}

How do I get these implicit conversions into scope?

You've misspelt your import statement; it should be import play.api.libs.json.Json (it's important to remember Scala is case-sensitive). Fixing that, the code works:

scala> :paste
// Entering paste mode (ctrl-D to finish)

import play.api.libs.json.Json
Json.obj("key" -> "value")

// Exiting paste mode, now interpreting.

import play.api.libs.json.Json
res0: play.api.libs.json.JsObject = {"key":"value"}

scala>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM