I´m using Scala.js and want to read a JSON i get from my backend.
My problem is, i don't know how to work with my response as json.
All examples i found use JSON.toJson(xhr.responseText) but this only works if i get one String (right?)
Also i dont want to parse the JSON in Objects (in this example Users)
I use the Json lib from the Play-Framework.
Example for Json:
[
{
"name": "User1",
"age": 18
},
{
"name": "User2",
"age": 18
},
{
"name": "User3",
"age": 18
}
]
My Code
val xhr = new dom.XMLHttpRequest()
xhr.open("GET", backend + "/ROUTE")
xhr.responseType="json"
xhr.onload = { (e: dom.Event) =>
println(xhr.response)
//What i want
// for (user<-response) println(user("age"),user("name"))
}
xhr.send()
The output is
[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object]
I tried things like
val js=Json.obj("users"->xhr.response)
and so on.
I guess i have a misunderstanding how exactly
xhr.responseType="json"
works but can't figure it out.
I know how i would do it in "normal" Play json("name")