在 Lift 中保存 Json 提取

发布于 2024-08-19 04:20:21 字数 1832 浏览 6 评论 0原文

我正在尝试保存与 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

稚气少女 2024-08-26 04:20:21

您可以使用 Object.create 创建建模类的实例,使用类对象的 apply 方法来设置实例的值,然后通过调用 save 来保留。

假设您想要设置这些值:

  sample1: "Fishsticks"
  sample2: "InYour"
  sample3: "Mouth"

然后您想要将模型保存到数据库中。

由于您可以链接函数调用,因此您可以非常简洁地执行此操作:

// Create the instance
var newDone = newDone.create

// Set it's instance variables
newDone.sample1("Fishsticks").sample2("InYour").sample3("Mouth")

// Persist it
newDone.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:

  sample1: "Fishsticks"
  sample2: "InYour"
  sample3: "Mouth"

You then want to save the model to the database.

Since you can chain function calls, you can do this very succinctly:

// Create the instance
var newDone = newDone.create

// Set it's instance variables
newDone.sample1("Fishsticks").sample2("InYour").sample3("Mouth")

// Persist it
newDone.save

So, once you've parsed your JSON data, you can use use the format above to persist it into your database.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文