2016-11-21 223 views
0

我想从一个列表中构建一个JSON对象,其中键是“products”,值是List [Product],其中Product是一个case类。但是我我得到错误,说“类型不匹配;发现:(String,List [com.mycompnay.ws.client.Product])required:net.liftweb.json.JObject(展开为)net.liftweb.json.JsonAST.JObject ”。转换为net.liftweb.json.JsonAST.JObject提升斯卡拉

我迄今所做的是如下:

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

回答

1

您正在寻找decomposedoc)。请参阅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"}]} 
+0

是的,完美您的回复helps.Thanks。 – Sakalya

+0

@Sakalya我很高兴能帮上忙。如果在一段时间后没有更好的答案,请考虑接受这个答案。谢谢 :) –