How can I construct and parse a JSON string in Scala / Lift

Posted by David Carlson on Stack Overflow See other posts from Stack Overflow or by David Carlson
Published on 2009-05-29T20:28:14Z Indexed on 2011/01/14 19:53 UTC
Read the original article Hit count: 250

Filed under:
|
|

I am using JsonResponse to send some JSON to the client.

To test that I am sending the correct response it seemed natural to me to parse the resulting JSON and validate against a data structure rather than comparing substrings.

But for some reason I am unable to parse the JSON I just constructed:

  def tryToParse = {
    val jsObj :JsObj = JsObj(("foo", "bar")); // 1)
    val jsObjStr :String = jsObj.toJsCmd      // 2) jsObjStr is: "{'foo': 'bar'}"
    val result = JSON.parseFull(jsObjStr)     // 3) result is: None

    // the problem seems to be caused by the quotes:
    val works = JSON.parseFull("{\"foo\" : \"bar\"}")  // 4) result is: Some(Map(foo -> bar))
    val doesntWork = JSON.parseFull("{'foo' : 'bar'}") // 5) result is: None
  }

How do I programmatically construct a valid JSON message in Scala/Lift that can also be parsed again?

© Stack Overflow or respective owner

Related posts about JSON

Related posts about scala