我需要捕获 json 的 int 值并通过某个映射表将其更改为字符串值。我使用 circe,我知道如何在不更改值类型的情况下修改值,例如:
package com.accenture.soh.driver
import io.circe._, io.circe.parser._
import cats.syntax.either._
import io.circe._, io.circe.parser._
import io.circe.optics.JsonPath._
/**
* Created by laurence.geng on 2017/2/2.
*/
object CirceTest extends App {
val json: Json = parse(
"""
[
{
"metric":"my-metric",
"dps":{"1484214238":5,"1484214239":1,"1484214240":4,"1484214241":11}
}
]
""").getOrElse(Json.Null)
val doubleValue: Json => Json = root.each.dps.each.int.modify(_ * 2)
println(doubleValue(json).toString())
}
Run Code Online (Sandbox Code Playgroud)
输出是:
[
{
"metric" : "my-metric",
"dps" : {
"1484214238" : 10,
"1484214239" : 2,
"1484214240" : 8,
"1484214241" : 22
}
}
]
Run Code Online (Sandbox Code Playgroud)
但是,实际上,我需要做的是将 int 值更改为某个字符串值,即 1 -> "A"、2 -> "B" 等等,问题是:“修改”方法只接受一个返回与输入值相同类型的函数,因此,我不能编码如下:
val intToString: Json => Json = root.each.dps.each.int.modify(_.toString)
Run Code Online (Sandbox Code Playgroud)
我的预期输出可能如下所示:
[
{
"metric" : "my-metric",
"dps" : {
"1484214238" : "10",
"1484214239" : "2",
"1484214240" : "8",
"1484214241" : "22"
}
}
]
Run Code Online (Sandbox Code Playgroud)
谁能给我一个解决方法(基于 Circe)?
您可以使用 circe 游标来完成,但我认为将其转换为 case 类并对其进行操作更容易。
val json =
"""
|{
| "metric":"my-metric",
| "dps":{"1484214238":5,"1484214239":1,"1484214240":4,"1484214241":11}
| }
""".stripMargin
val doc = parse(json).getOrElse(Json.Null)
val cursor = doc.hcursor
val dps = cursor.downField("dps")
val result = dps.withFocus { json: Json =>
json.mapObject { jsonObject =>
JsonObject.fromMap(jsonObject.toMap.mapValues { x =>
Json.fromString(x.asNumber.get.toString)
})
}
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
2145 次 |
| 最近记录: |