如何“压平”复合对象的 JSon 表示?

发布于 2024-11-26 02:51:35 字数 338 浏览 2 评论 0原文

假设我有以下结构,我想在 Json 中序列化:

case class A(name:String)

case class B(age:Int)

case class C(id:String, a:A,b:B)

我正在使用 lift-json "write(...)" ,但我想展平结构,而不是:

{ id:xx , a:{ name:"xxxx" }, b:{ age:xxxx } }

我想得到:

{ id:xx , name:"xxxx" , age:xxxx  }

Suppose I have the following structure I want to serialize in Json:

case class A(name:String)

case class B(age:Int)

case class C(id:String, a:A,b:B)

I'm using lift-json "write(...)" , but I want to flatten the structure so instead of:

{ id:xx , a:{ name:"xxxx" }, b:{ age:xxxx } }

I want to get:

{ id:xx , name:"xxxx" , age:xxxx  }

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(3

薆情海 2024-12-03 02:51:35

JValue 上使用 transform 方法:

import net.liftweb.json._
import net.liftweb.json.JsonAST._
implicit val formats = net.liftweb.json.DefaultFormats
val c1 = C("c1", A("some-name"), B(42))
val c1flat = Extraction decompose c1 transform  { case JField(x, JObject(List(jf))) if x == "a" || x == "b" => jf }
val c1str = Printer pretty (JsonAST render c1flat)

结果:

c1str: String =
{
  "id":"c1",
  "name":"some-name",
  "age":42
}

Use transform method on JValue:

import net.liftweb.json._
import net.liftweb.json.JsonAST._
implicit val formats = net.liftweb.json.DefaultFormats
val c1 = C("c1", A("some-name"), B(42))
val c1flat = Extraction decompose c1 transform  { case JField(x, JObject(List(jf))) if x == "a" || x == "b" => jf }
val c1str = Printer pretty (JsonAST render c1flat)

Result:

c1str: String =
{
  "id":"c1",
  "name":"some-name",
  "age":42
}
铜锣湾横着走 2024-12-03 02:51:35

如果 A 和 B 有多个字段,您将需要稍微不同的方法:

import net.liftweb.json._
import net.liftweb.json.JsonAST._
import net.liftweb.json.JsonDSL._

implicit val formats = net.liftweb.json.DefaultFormats
implicit def cToJson(c: C): JValue = (("id" -> c.id):JValue) merge (Extraction decompose c.a) merge (Extraction decompose c.b)
val c1 = C("c1", A("a name", "a nick", "an alias"), B(11, 111, 1111))
Printer pretty (JsonAST render c1)
res0: String =
{
  "id":"c1",
  "name":"a name",
  "nick":"a nick",
  "alias":"an alias",
  "age":11,
  "weight":111,
  "height":1111
}

If A and B have multiple fields you will want a slightly different approach:

import net.liftweb.json._
import net.liftweb.json.JsonAST._
import net.liftweb.json.JsonDSL._

implicit val formats = net.liftweb.json.DefaultFormats
implicit def cToJson(c: C): JValue = (("id" -> c.id):JValue) merge (Extraction decompose c.a) merge (Extraction decompose c.b)
val c1 = C("c1", A("a name", "a nick", "an alias"), B(11, 111, 1111))
Printer pretty (JsonAST render c1)
res0: String =
{
  "id":"c1",
  "name":"a name",
  "nick":"a nick",
  "alias":"an alias",
  "age":11,
  "weight":111,
  "height":1111
}
巴黎夜雨 2024-12-03 02:51:35

您可以使用字段(id、name、age)声明一个新对象 D,并在构造函数中加载所需的值,然后将该类序列化为 json。可能还有另一种方法,但这种方法会起作用。

You can declare a new object D with fields (id, name, age) and load the values you want in the constructor then serialize that class to json. There may be another way but this way will work.

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