【问题标题】:Convert scala case class to json将scala案例类转换为json
【发布时间】:2016-07-27 03:24:05
【问题描述】:

我需要将 scala 案例类转换为 json。我正在使用喷雾 json。 问题是我的案例类有超过 22 个字段,并且喷雾 json 仅支持 22 个字段。

我写不出来

implicit val someformat = jsonformat25(Somecaseclass)

case class Color(name: String, red: Int, green: Int, blue: Int)

object MyJsonProtocol extends DefaultJsonProtocol {
  implicit val colorFormat = jsonFormat4(Color)
}

import MyJsonProtocol._
import spray.json._

val json = Color("CadetBlue", 95, 158, 160).toJson
val color = json.convertTo[Color]

我看到有并尝试使用它,但它不起作用。请提出一些解决方案。

implicit object format extends RootJsonFormat[SomeObject] {
    def write(a: SomeObject) = a match {
      case p: SomeObject=> p.toJson
    }
    def read(value: JsValue) = {
      value.convertTo[SomeObject]
    }
  }

请不要建议拆分类参数并制作嵌套案例类。我的要求是我在一个案例类中有超过 22 个字段。

【问题讨论】:

标签: json scala spray


【解决方案1】:

你可以使用 - https://github.com/sitetester/json-handler

  • 它易于使用,并支持将嵌套案例类转换为带有适当缩进的 JSON。

【讨论】:

  • 您提供的库不会以某种方式生成有效的 JSON,有等号,没有逗号等
猜你喜欢
  • 1970-01-01
  • 2014-12-18
  • 1970-01-01
  • 2014-01-08
  • 1970-01-01
  • 2013-04-13
  • 1970-01-01
  • 1970-01-01
  • 2015-06-16
相关资源
最近更新 更多