使用 Lift-JSON 解析嵌套 JSON 值时出现问题
Issue with parsing nested JSON values with Lift-JSON
我正在使用 Scala 2.12 并尝试解析以下 JSON 文件。
{
"comp1": {
"metrics": {
"operation1": {
"alias": "activity_operation",
"weight": 10
},
"operation2": {
"alias": "service_operation",
"weight": 22
}
}
},
"comp2": {
"metrics": {
"operation1": {
"alias": "activity_operation",
"weight": 14
},
"operation4": {
"alias": "service_operation",
"weight": 16
}
}
}
}
我已将 json 加载到配置变量中,定义了一个案例 class 并尝试了以下操作:
case class OperationDetails(alias: String, weight: Int)
for (detail <- (config \ "comp1").children) {
println(detail.extract[OperationDetails])
}
这给我错误 Exception in thread "main" net.liftweb.json.MappingException: No usable value for alias. Did not find value which can be converted into java.lang.String
我无法使用“operation1”检索子项,因为操作是随机的。
我需要检索操作名称 operation1, operation2, operation4, ..
及其各自的别名和权重。有什么想法吗?
您至少缺少一层嵌套,并且可能还缺少隐式 val formats
。
这将打印所有操作。请注意转换为 JObject
以便能够检索字段名称。
// Setup
case class Operation(alias: String, weight: Int)
implicit val formats = DefaultFormats
// Traversal
val comps: List[JsonAST.JValue] = config.children
for (comp <- comps) {
val metrics:List[JsonAST.JValue] = comp.children
for (metric <- metrics) {
val operations:List[JsonAST.JField] = metric.asInstanceOf[JObject].obj
for (operation <- operations) {
val op = operation.value.extract[Operation]
// Do something here
println(s"${operation.name}:(${op.alias},${op.weight})")
}
}
}
输出:
operation1:(activity_operation,10)
operation2:(service_operation,22)
operation1:(activity_operation,14)
operation4:(service_operation,16)
我正在使用 Scala 2.12 并尝试解析以下 JSON 文件。
{
"comp1": {
"metrics": {
"operation1": {
"alias": "activity_operation",
"weight": 10
},
"operation2": {
"alias": "service_operation",
"weight": 22
}
}
},
"comp2": {
"metrics": {
"operation1": {
"alias": "activity_operation",
"weight": 14
},
"operation4": {
"alias": "service_operation",
"weight": 16
}
}
}
}
我已将 json 加载到配置变量中,定义了一个案例 class 并尝试了以下操作:
case class OperationDetails(alias: String, weight: Int)
for (detail <- (config \ "comp1").children) {
println(detail.extract[OperationDetails])
}
这给我错误 Exception in thread "main" net.liftweb.json.MappingException: No usable value for alias. Did not find value which can be converted into java.lang.String
我无法使用“operation1”检索子项,因为操作是随机的。
我需要检索操作名称 operation1, operation2, operation4, ..
及其各自的别名和权重。有什么想法吗?
您至少缺少一层嵌套,并且可能还缺少隐式 val formats
。
这将打印所有操作。请注意转换为 JObject
以便能够检索字段名称。
// Setup
case class Operation(alias: String, weight: Int)
implicit val formats = DefaultFormats
// Traversal
val comps: List[JsonAST.JValue] = config.children
for (comp <- comps) {
val metrics:List[JsonAST.JValue] = comp.children
for (metric <- metrics) {
val operations:List[JsonAST.JField] = metric.asInstanceOf[JObject].obj
for (operation <- operations) {
val op = operation.value.extract[Operation]
// Do something here
println(s"${operation.name}:(${op.alias},${op.weight})")
}
}
}
输出:
operation1:(activity_operation,10)
operation2:(service_operation,22)
operation1:(activity_operation,14)
operation4:(service_operation,16)