Download a Json file

classic Classic list List threaded Threaded
5 messages Options
Reply | Threaded
Open this post in threaded view
|

Download a Json file

Randin
Hello again, another basic question, I need to download a .json file, the way i have it written its trying to crawl the site. Here is the code....

package weather.snippet


import dispatch._
import dispatch.json.Js._
import Http._

class HelloWorld {

val http = new Http
val db = :/("www.bom.gov.au")

  object Rawdata extends dispatch.json.Js {
val desc = 'mainId ? str
val id = 'ID ? str
val name = 'name ? str
val stateTimeZone = 'stateTimeZone ? str
val timeZone = 'timeZone ? str
val productName = 'productName ? str
val state = 'state ? str
val sortOrder = 'sortOrder ? num
val wmo = 'wmo ? num
val historyProduct = 'historyProduct ? str
    val localDateTime = 'localDateTime ? str
    val localDateTimeFull = 'localDateTimeFull ? str
    val airTemp = 'airTemp ? num
    val dewPt = 'dewPt ? num
    val apparentT = 'apparentT ? num
    val relHum = 'relHum ? num
    val deltAt = 'deltAt ? num
    val windDir = 'windDir ? num
    val windSpdKmh = 'windSpdKmh ? num
    val gustKmh = 'gustKmh ? num
    val press = 'press ? num
    val rainTrace = 'rainTrace ? num
}

val rawdata = http(db / "/fwo/IDV60901/IDV60901.94868.json" ># (list ! obj))

  def howdy = println(rawdata)
}
Reply | Threaded
Open this post in threaded view
|

Re: Download a Json file

Randin
Any info you can give me would be great, thank you in advance.
Reply | Threaded
Open this post in threaded view
|

Re: Download a Json file

Randin
The absence  of a reply leads me to believe it cannot be done with Dispatch, it seems I need to look into java.io then.
Reply | Threaded
Open this post in threaded view
|

Re: Download a Json file

n8han
Administrator
A lot of things are possible, you just have to read through the javadocs and source to know them. There are >> and >>> operators defined on Request; the first takes a function to handle a stream, the second takes an outputstream and does the work of copying for you. So,

http(myRequest >>> new FileOutputStream(new File("/path/to/myfile")))

should pretty much do it.
Reply | Threaded
Open this post in threaded view
|

Re: Download a Json file

Randin
Great! Thank you for your time and patience in this matter, here is what we came up from your example...


val myRequest = new Request("http://www.bom.gov.au/fwo/IDV60901/IDV60901.94868.json")
val rawdata = http(myRequest >>> bos)
val bs = bos.toString