Play scalaJson how to retrieve a record when queried for a value

So I have a couple of Play scalaJSON problems. The first is that I somehow can't make my keys something other than strings. I have defined the JSONWrites converter as follows:

implicit val musics = new Writes[Question] {
      def writes(question:Question) = Json.obj(
        "questionID" -> question.questionID,
        "questionText" -> question.questionText,
        "responseURI" -> question.responseURI,
        "constraints: min,max,Optional" -> Json.arr(question.minResponse, question.maxResponse, question.optionalQ),
        "responseDataType" -> question.responseDataType
      )
    }

      

my case class for the question:

case class Question (questionID:Int,
                      questionText:String,
                      responseURI:String,
                      minResponse:Option[Int],
                      maxResponse:Option[Int],
                      optionalQ:Boolean,
                      responseDataType:String)

      

When I was planning on using the REST API in a game, I wanted to get a specific question about this poll with a url like /questionBlock/questionID

I tried to just make question.questionID the parent key and insert the rest of the JSON as the value of that key, but that wouldn't allow me to do that by saying expected String actual Int

the actual JSON output looks like this:

  [{"questionID":0,"questionText":"What is your favorite musical artist?",
"responseURI":"/notDoneYet","constraints: min,max,Optional":[1,1,false],
"responseDataType":"String"},{"questionID":1,"questionText":"What is your favorite music genre?",
"responseURI":"/notDoneYet","constraints: min,max,Optional":[1,1,false],"responseDataType":"String"}]

      

But with this, I can't figure out how to return the entire field where questionID is 1 or 2, etc. I used item 0, 1, etc. from an array, but this is not an ideal approach for me as Question IDs may not always start at 0 for a specific sequence of questions.

Basically, I want to show the entire post for a single question when I provide a questionID value. In Javascript, I would choose a foreign key for the questionID value, but I can't figure out how to do this using scalaJson. If there is an alternative way to achieve this, I am open to suggestions.

+3
json scala playframework-2.0


source to share


No one has answered this question yet

Check out similar questions:

202
How can I change the default port (9000) that is used when using the "start" command?
58
How to handle optional request parameters in the Play platform
17
Outputting "null" for option [T] in play-json serialization when value is None
sixteen
How to replace JSON value in Play
8
Routing based on a query parameter in the replay structure
2
Play Json - Conflict while simulating deserialization
2
Play Framework JSON Method Overloaded [apply]
1
Playback framework: urls in ajax requests
1
check multiple json values ​​in game
0
Ajax Form - get information



All Articles
Loading...
X
Show
Funny
Dev
Pics