在 Lift 中保存 Json 提取
我正在尝试保存与 Lifts 的 Json 数据提取相关的数据并将其保存到数据库(通过映射器),但我找不到将片段连接到映射器的位置。代码如下所示。
这是一个测试片段。
package com.testjson.snippet
import dispatch._
import net.liftweb.json.JsonParser._
import java.io.{ByteArrayOutputStream}
import com.testjson.model.Done
class HelloWorld {
def howdy = <span>Welcome to hello-lift at {new _root_.java.util.Date}</span>
val http = new Http
val bos = new ByteArrayOutputStream
val myRequest = new Request("http://testing.com/folder/file.json")
val rawdata = http(myRequest >>> bos)
val bs = bos.toString
val json = parse(bs)
implicit val formats = net.liftweb.json.DefaultFormats
case class One(sample1: String, sample2: String, sample3: String)
case class Two(samplea: String, sampleb: String, samplec: String, sampled: String)
case class Three(alpha: Int, beta: Int, charlie: String, delta: String)
case class Done(notice: List[One], header: List[Two], data: List[Three])
json.extract[Done]
}
这里是一个示例模型。
package com.testjson.model
import net.liftweb.http.SHtml
import net.liftweb.common._
import net.liftweb.mapper._
class Done extends LongKeyedMapper[Done] with IdPK {
def getSingleton = Done
object sample1 extends MappedPoliteString(this, 12)
object sample2 extends MappedPoliteString(this, 12)
object sample3 extends MappedPoliteString(this, 56)
object samplea extends MappedPoliteString(this, 12)
object sampleb extends MappedPoliteString(this, 12)
object samplec extends MappedPoliteString(this, 56)
object alpha extends MappedPoliteString(this, 56)
object beta extends MappedInt(this)
object charlie extends MappedInt(this)
object delta extends MappedPoliteString(this, 56)
}
object Done extends Done with LongKeyedMetaMapper[Done]
我已经浏览了这本书的印刷版、更新的电子书和谷歌小组,但无济于事。我只是在寻找一些帮助或者代码示例来为我指明正确的方向。
I'm trying to save data parced with Lifts' Json data extraction and save it to the database(via mapper) but I cannot find where to connect the snippit to the mapper. The code looks like this.
Here is a test snippit.
package com.testjson.snippet
import dispatch._
import net.liftweb.json.JsonParser._
import java.io.{ByteArrayOutputStream}
import com.testjson.model.Done
class HelloWorld {
def howdy = <span>Welcome to hello-lift at {new _root_.java.util.Date}</span>
val http = new Http
val bos = new ByteArrayOutputStream
val myRequest = new Request("http://testing.com/folder/file.json")
val rawdata = http(myRequest >>> bos)
val bs = bos.toString
val json = parse(bs)
implicit val formats = net.liftweb.json.DefaultFormats
case class One(sample1: String, sample2: String, sample3: String)
case class Two(samplea: String, sampleb: String, samplec: String, sampled: String)
case class Three(alpha: Int, beta: Int, charlie: String, delta: String)
case class Done(notice: List[One], header: List[Two], data: List[Three])
json.extract[Done]
}
And here a sample Model.
package com.testjson.model
import net.liftweb.http.SHtml
import net.liftweb.common._
import net.liftweb.mapper._
class Done extends LongKeyedMapper[Done] with IdPK {
def getSingleton = Done
object sample1 extends MappedPoliteString(this, 12)
object sample2 extends MappedPoliteString(this, 12)
object sample3 extends MappedPoliteString(this, 56)
object samplea extends MappedPoliteString(this, 12)
object sampleb extends MappedPoliteString(this, 12)
object samplec extends MappedPoliteString(this, 56)
object alpha extends MappedPoliteString(this, 56)
object beta extends MappedInt(this)
object charlie extends MappedInt(this)
object delta extends MappedPoliteString(this, 56)
}
object Done extends Done with LongKeyedMetaMapper[Done]
I've looked through my print copy of the book, the updated ebook, and the google group to no avail. I'm just looking for some help or maybe a code example to point me in the right direction.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您可以使用 Object.create 创建建模类的实例,使用类对象的 apply 方法来设置实例的值,然后通过调用 save 来保留。
假设您想要设置这些值:
然后您想要将模型保存到数据库中。
由于您可以链接函数调用,因此您可以非常简洁地执行此操作:
因此,解析 JSON 数据后,您可以使用上面的格式将其保存到数据库中。
You create instances of a modeled class using Object.create, use the apply methods of the class' objects to set the instance's values, then persist by calling save.
Say you wanted to set these values:
You then want to save the model to the database.
Since you can chain function calls, you can do this very succinctly:
So, once you've parsed your JSON data, you can use use the format above to persist it into your database.