arrays - Json4s 将 JValue 添加到 JArray

标签 arrays json scala json4s

我正在使用 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/

相关文章:

c - C语言中如何删除字符串中的特殊字符

java - 在 Scala 中解析具有 ISO 格式的日期时间

ruby-on-rails - 关于 Rails 前端和 Scala 后端之间通信的建议

java - 如何从该字符串创建 JSONArray?

c++ - 为什么在使用 "typeid"时必须输入数组长度?

c# - 如何将此字符串拆分为数组?

java - 如何使用 GSON 从该 JSON 生成 Java 对象?

android - 在 onCreate 之前将来自 mySQL 的信息放入 Fragment textView 中

json - 如何从 pm2 日志中删除应用程序 ID 和名称?

scala - 这个尾递归斐波那契函数的定义是尾递归的吗?