4

I want to convert hashmap into json object, my hashmap structure is look like this:

def res=Action{ implicit request=>
  var response=new HashMap[String,Map[String,String]]
  response=//etc .......
  .
  .
  .
  Ok(write(response))
}

bt its not working .

2
  • Take a look at the json utilities built into play framework, then apply the knowledge Commented Apr 7, 2014 at 11:22
  • it depends upon your requirement... can you show me which type format u want to get ? Commented Apr 8, 2014 at 12:06

4 Answers 4

4

Try this:

Ok(Json.toJson(response.toMap))

This would convert your HashMap to a Map which can be written as json without additional code.

Sign up to request clarification or add additional context in comments.

8 Comments

i tried it but it giving me can not write an instanse of com.fasterxml.jackson.databind.jsonNode to http response try to define a writable[com.fasterxml.jackson.databind.jsonNode]
It seems you imported the wrong package. Make sure you imported play.api.libs.json.Json
its not working properly its giving me {"keyValue":"LoadFactor:0.75"....not giving key's Value
import net.liftweb.json.Serialization.write ,import net.liftweb.json.DefaultFormats, import play.api.libs.json.Json
Why would you use java.util.Map? scala.collection.immutable.Map is the only Map that can be serialized without implementing writes or formats
|
3

An alternative solution would be to use JSON4s. [https://github.com/json4s/json4s] As additional gain it gives you a nice DSL, the abilitity to use Jackson or not and a great way to deserialize JSON.

scala> import org.json4s.jackson.Serialization
import org.json4s.jackson.Serialization

scala> implicit val formats = Serialization.formats(NoTypeHints)
formats: org.json4s.Formats{val dateFormat: org.json4s.DateFormat; val typeHints:org.json4s.TypeHints} = org.json4s.Serialization$$anon$1@f40c08d

scala> Serialization.write(Map("test" -> "a", "test 2" -> 2))
res1: String = {"test":"a","test 2":2}

Comments

1

Try like this

val data = response.map(value=> value._1 -> Json.toJson(value._2))

Ok(json.toJson(data.toMap))

2 Comments

its giving compile time error ............No json deserializer found for type java.util.map[String,String] try implement implicit write or format for this type.
then try convert map to list like response.map(value=> value._1 -> Json.toJson(value._2.toList))
0
You can try like this....

var ls: ListBuffer[(String, Map[String, String])] = ListBuffer()
val res = list1.toList.iterator

while (res.hasNext) {

  ls += (("id", getMyMap().toMap))
}
println(ls);
ls.toList
ok(write(ls.toMap))


def getMyMap(): scala.collection.mutable.Map[String, String] = {

var m=scala.collection.mutable.Map("Address" -> "strt1", "Mobile" -> 98974)
 m
}

Output:

{"0":{"Address":"strt1","Mobile":"98974"}}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.