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


source to share





All Articles