scala - 使用 Scala 和 JSON4S 操作 JSON 中的数组

标签 scala json4s

我正在尝试使用 Json4s 在 Scala 中操作 JSON 对象。

假设我的 JSON 如下所示:

{
  "data": {
    "plan": {
      "itineraries": [
       {
         "startTime": 1494933289000,
         "duration": 2174
       },
       {
         "startTime": 2494933289000,
         "duration": 3174
       }
     ]
    }
  }
}

假设我想为每个行程添加一个字段 - 像这样:

{
  "data": {
    "plan": {
      "itineraries": [
       {
         "startTime": 1494933289000,
         "duration": 2174,
         "id": "1"
       },
       {
         "startTime": 2494933289000,
         "duration": 3174,
         "id": "2"
       }
     ]
    }
  }
}

我该怎么做?

最佳答案

有阅读器案例类:ItineraryReads(startTime,uration)

Writer 案例类:ItineraryWrites(startTime、duration、id)

def mapToWrites(obj : List[ItineraryReads]) = {

obj.map(o => ItineraryWrites(o.startTime, o.duration, (obj.indexOf(o)) + 1))

}

这应该有效。将其写为 ItineraryWrites 列表:)

关于scala - 使用 Scala 和 JSON4S 操作 JSON 中的数组,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44279551/

相关文章:

scala - 模式匹配期间需要稳定的标识符吗? (斯卡拉)

json - 使用 Json4s 和自定义序列化器多态读取 JSON

scala - JArray 中的 TransformField 与 json4s

json - 如何在 Spark 2 Scala 中将 Row 转换为 json

json - scala - 使用 json4s 分解时如何更改字段名称?

json - ObjectId 未序列化为 JSON

python - Scala HTTPS post 包括 JSON 输入和输出以及身份验证

scala - RoundRobinPool 和 RoundRobinRouter 有什么区别

git - sbt + Intellij IDEA : dependencies from git?

scala - Gradle 在另一个子项目中使用的子项目的传递依赖项