Write a map with key as int to json in scala using json4s -
i trying write map
in key int
json string not able so:
import org.json4s._ import org.json4s.jackson.jsonmethods._ import org.json4s.jsondsl._ object myobject { def main(args: array[string]) { // works fine //val mymap = map("a" -> list(3,4), "b" -> list(7,8)) // not work val mymap = map(4 -> map("a" -> 5)) val jsonstring = pretty(render(mymap)) println(jsonstring) }
i receiving following error:
[error] /my_stuff/my_file.scala:14: overloaded method value render alternatives: [error] (value: org.json4s.jvalue)org.json4s.jvalue <and> [error] (value: org.json4s.jvalue)(implicit formats: org.json4s.formats)org.json4s.jvalue [error] cannot applied (scala.collection.immutable.map[int,scala.collection.immutable.map[string,int]]) [error] val jsonstring = pretty(render(mymap)) [error] ^ [error] 1 error found [error] (compile:compileincremental) compilation failed
i vaguely understand error message, looks render expects jvalue input, , not providing it, don't first case either, , code works expect.
how write such map json string?
edit: source of confusion
i python programmer, , in python
in [1]: import json in [2]: wrong = {2: 5} in [3]: open("wrong.json") f: ...: json.dump(wrong, f)
works fine, of course python stringifies 2
.
i think expected result. if check json specification see need use strings names of elements.
so afraid need like:
val mymap = map("4" -> map("a" -> 5))
Comments
Post a Comment