-1

Creating Json is easy with play framework / scala. here is an example about it ,

 import play.api.libs.json.Json

val json: JsValue = Json.parse("""
{ 
  "user": {
    "name" : "toto",
    "age" : 25,
    "email" : "[email protected]",
    "isAlive" : true,
    "friend" : {
      "name" : "tata",
      "age" : 20,
      "email" : "[email protected]"
    }
  } 
}
""")

For more information and source : http://www.playframework.com/documentation/2.1.1/ScalaJson

0

1 Answer 1

3

I guess you just want an array, not an object:

Json.arr(
  Json.obj(
    "id" -> "node37",
    "name" -> 3.7,
    "data" -> Json.obj(),
    "children" -> Json.arr()
  ),
  Json.obj(
    "id" -> "node38",
    "name" -> 3.8,
    "data" -> Json.obj(),
    "children" -> Json.arr()
  )
)

Which gives

 [{
      "id": "node37",
      "name": "3.7",
      "data": {},
      "children":[]
 },
 {
      "id": "node38",
      "name": "3.8",
      "data": {},
      "children":[]
 }]
Sign up to request clarification or add additional context in comments.

9 Comments

This is an improvement , i also need to have this "id"and other fields without "" .
That is not correct JSON. Why do you need to generate invalid JSON?
Because i will use this library in order to implement a graph.Like here link
I don't see why it is required... Don't you use JSON.parse() in the browser? Still it is possible to declare such an object in JS: var foo = {"id": 1234}
I don't know how can i use that , can you give me an example about that the syntax of what i want is obvious then what i am going to do with Json.Parse() ?
|

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.