autodata 3.39 2012 srpski jezik
autodata 3.39 2012 torrent
autodata 3.39 2012 srpski jezik
autodata 3.39 2012 srpski jezik nas.. autodata 3.39 2012 srpski jezik nas. selekcija tablic.
autodata 3.39 2012,Srpski Jezik,Srpski Jezik,Srpski Jezik Nas downloadautodata 3.39 2012 free.autodata 3.39 2012 download srpski.Q:
Scala - Convert JSON to an Option[T] type
I'm new to Scala and I'm currently working on a project that deals with MongoDB.
I have a JSON response that looks something like this:
[{"A":"aa", "B":"bb"}, {"C":"cc"}]
And I'm trying to create an object that has one of the values as a string and the other one as a default value, which would look something like this:
{"A":"aa","B":"bb","C":"cc"}
Is there a way to convert the JSON response to that type? I'm using a combination of Scala Play and MongoDB.
A:
Use the following code:
import com.mongodb.util.JSON
// construct a JSON from your json
val js: JSON =
(JSON / "A").asOpt[String]
.orElse(JsNull)
.orElse(JsEmpty)
.getOrElse(JsEmpty)
// show the value
println(js)
A:
If you are using play framework:
In the routes file:
POST /some/url/handler
{
"one": 1,
"two": 2
}
Then the handler:
def handler(request: RequestHeader, json: JsValue) = {
json.validate[Map[String, String]] match {
case JsSuccess(Map(k1 -> s1, k2 -> s2)) => Map(k1 -> s1, k2 -> s2)
case _ => Map("one" -> "invalid") ac619d1d87
Related links:
Comments