« Return to Thread: outputting json

Re: outputting json

by Josh Joy-3 :: Rate this Message:

Reply to Author | View in Thread

kewl, thanks

On Sun, Aug 3, 2008 at 9:19 PM, David Pollak <feeder.of.the.bears@...> wrote:
By default, a JsExp (JavaScript Expression) is a subclass of scala.xml.Node.  The reasons take a little bit of explaining, but in order for the JsExp to properly output itself as part of an XML document, it must put itself in a comment.

To get what you want, you can do the following:

object  Test  {

  def main(args: Array[String]) {
      val value = "josh"
      val result = JsObj("Foo" -> value, "bar" -> "baz", "rabbit" -> JsArray(44, 66, "Dog", JsObj("yep" -> true)))
      println(result.toJsCmd)
    }
}

Note two things:
  • The toJsCmd converts a JsCmd (JavaScript Command) or JsExp to a String
  • the curly braces are not necessary around "value"  The "DSL" for JavaScript object creation doesn't require any different escaping than does any other Scala expression.
Thanks,

David



On Sun, Aug 3, 2008 at 6:28 PM, Josh Joy <joshjdevl@...> wrote:
Ok cool. This looks like what I need.

One more question, I did the following,


import net.liftweb.http.js._
import JE._
import JsCmds._

object  Test  {

  def main(args: Array[String]) {
      val value = "josh"
      val result = JsObj("Foo" -> {value}, "bar" -> "baz", "rabbit" -> JsArray(44, 66, "Dog", JsObj("yep" -> true)))
      println(result)
    }
   
}


The output is
<!-- {'Foo': 'josh', 'bar': 'baz', 'rabbit': [44, 66, 'Dog', {'yep': true}
]
}

-->

Is there a reason for the <!-- --> ? Can I disable that?

Thanks,
Josh


On Sun, Aug 3, 2008 at 6:48 PM, David Pollak <dpp@...> wrote:
import net.liftweb.http.js._
import JE._
import JsCmds._

    JsObj("Foo" -> 55, "bar" -> "baz", "rabbit" -> JsArray(44, 66, "Dog", JsObj("yep" -> true)))


Josh Joy wrote:
Could you please be more specific for new people like me?
See the above code


The link you provided goes to the Lift main web site. I used the wiki search for JSON and also searched Google, the closest thing I could find was JsonHandler .

Is that what you are referring to?

Yea...googling scala, lift, liftweb, json, jsonhandler (all various combinations of these keywords), doesn't really turn up much...
I'm not sure what I missed on the liftweb homepage...

I'm just looking to integrate a nice and nifty json builder in an existing spring mvc app...


You should use lift to build web apps.  It's about 50 times better and easier than Spring.


I would use groovy, though from what I can see so far Scala is a little bit faster than groovy, though in this case if scala doesnt have these features then I'll prolly just use groovy...


Scala's faster.  Scala's radically better for building maintainable code.  And, as I said, there's an excellent JSON builder in lift.

Please keep the discussions on the mailing.  There are lots of people here to help.  The lift list ( http://groups.google.com/group/liftweb?hl=en ) is the best place to get help with web based apps in Scala.

Thanks,

David


Thanks,
Josh

On Sun, Aug 3, 2008 at 4:46 PM, David Pollak <dpp@...> wrote:
As I said, lift has a JSON build.


Josh Joy wrote:
Similar to the XML builder, how hard would it be implement a separate JSON builder in Scala?

I would like to do something similar that Groovy has in scala
http://www.jroller.com/aalmiray/entry/building_json_with_groovy

Thanks,
Josh



On Sat, Aug 2, 2008 at 8:06 PM, David Pollak <dpp@...> wrote:
Josh,

There's no way to convert from XML to JSON because XML contains sequences not expressible in JSON (e.g., multiple tags with the same name, attributes.)

lift (http://liftweb.net ) has JavaScript objects that will render themselves as strings.

Thanks,

David


Josh Joy wrote:
Hi,

I'm looking for a way to output (build) json.

For example, I see there is a way to build xml
http://www.scala-lang.org/intro/xml.html
object XMLTest2 extends Application {
 import scala.xml._
 val df = java.text.DateFormat.getDateInstance()

 val dateString = df.format(new java.util.Date())
 def theDate(name: String) =  <dateMsg addressedTo={ name }>
   Hello, { name }! Today is { dateString }

 </dateMsg>;
 println(theDate("John Doe").toString())
}

Is there a way to output JSON rather than xml?


Something similar to what I"m looking for is found in groovy
http://www.jroller.com/aalmiray/entry/building_json_with_groovy


Thanks,
Josh
 







--
lift, the simply functional web framework http://liftweb.net
Collaborative Task Management http://much4.us
Follow me: http://twitter.com/dpp
Git some: http://github.com/dpp

 « Return to Thread: outputting json