【问题标题】:Converting to net.liftweb.json.JsonAST.JObject Lift in Scala在 Scala 中转换为 net.liftweb.json.JsonAST.JObject Lift
【发布时间】:2016-11-21 19:53:22
【问题描述】:

我正在尝试从一个列表构造一个 JSON 对象,其中键是“产品”,值是列表 [产品],其中产品是案例类。但我收到错误消息,显示“类型不匹配;找到:(字符串, List[com.mycompnay.ws.client.Product]) 必需:net.liftweb.json.JObject(扩展为)net.liftweb.json.JsonAST.JObject"。

到目前为止我所做的如下:

val resultJson:JObject = "products" -> resultList
      println(compact(render(resultJson)))

【问题讨论】:

    标签: scala lift lift-json


    【解决方案1】:

    您正在寻找decompose (doc)。见this answer

    我测试了以下代码,它运行良好:

    import net.liftweb.json._
    import net.liftweb.json.JsonDSL._
    import net.liftweb.json.Extraction._
    
    implicit val formats = net.liftweb.json.DefaultFormats
    
    case class Product(foo: String)
    
    val resultList: List[Product] = List(Product("bar"), Product("baz"))
    val resultJson: JObject = ("products" -> decompose(resultList))
    println(compact(render(resultJson)))
    

    结果:

    {"products":[{"foo":"bar"},{"foo":"baz"}]}
    

    【讨论】:

    • 是的,这完全有帮助。感谢您的回复。
    • @Sakalya 我很高兴能帮上忙。如果一段时间后没有更好的答案,请考虑接受此答案。谢谢:)
    猜你喜欢
    • 2015-04-30
    • 1970-01-01
    • 2013-07-08
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-11-19
    • 2013-04-04
    • 2018-06-06
    相关资源
    最近更新 更多