我正在使用 scala 和 Json4s 以这种方式在循环中将 JValue 添加到 JArray:
var recordsJArray = JArray
for (record <- recordsList) {
val mrecord= new com.google.gson.Gson().toJson(record.asMap())
val jsonRecord = parse(mrecord)
recordsJArray = recordsJArray.++jsonRecord
}
我搜索了 de api: https://static.javadoc.io/org.json4s/json4s-core_2.9.1/3.0.0/org/json4s/JsonAST $$JArray.html
我已经尝试使用这个方法:
def ++ (other: JValue): JValue
但它不起作用。
error: value ++ is not a member of object org.json4s.JsonAST.JArray
[ERROR] recordsJArray = recordsJArray++jsonRecord
有人可以帮帮我吗? 有没有办法将 JValue 添加到 JArray? 谢谢
最佳答案
++
返回 JValue
,它是 JArray
的父类(super class)型。
由于 recordsJArray
必须是 JArray
,Scala 编译器正在寻找返回 JArray< 的
。由于没有,编译器报告您想要的函数不是您尝试调用它的对象的成员。++
的实现
你可以窃取their implementation of ++
for your input types,而不是依赖于JsonAST 的功能。 (JArray 加 JValue):
recordsJArray = JArray(recordsJArray ::: List(jsonRecord))
关于arrays - Json4s 将 JValue 添加到 JArray,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43780740/