json arrays in arrays 用 scala 和 lift 解析
json arrays in arrays parsing with scala and lift
仍然是 Scala 的菜鸟,我在这里碰壁了。
使用 Scala 2.12.1 和 liftweb 3.0.1
我有json代码
{
"traces":[
[
{
"matcher":"Matcher1",
"score":{
"assigned":50,
"max":50
}
},
{
"matcher":"Matcher2",
"score":{
"assigned":50,
"max":50
}
},
{
"matcher":"Matcher3",
"score":{
"assigned":50,
"max":50
}
}
],
[
{
"matcher":"Matcher4",
"score":{
"assigned":50,
"max":50
}
},
{
"matcher":"Matcher5",
"score":{
"assigned":50,
"max":50
}
},
{
"matcher":"Matcher6",
"score":{
"assigned":50,
"max":50
}
}
]
]
}
我正在尝试使用以下几行将其解析为某些案例分类
case class Traces (traces: List[List[Trace]])
case class Trace (matcher: String, score: Score)
val result = parse(json).extract[Traces]
我知道我的想法有些错误,但我似乎无法找到一种有效解析 json 的方法。
任何能为我指明正确方向的帮助将不胜感激。
[编辑]
我开始明白我确实在这里问错了我的问题。问题是我的示踪剂列表确实得到了 Nil。经过进一步调查,我发现我确实将我的嵌套 class 跟踪器放在了主要 class 的错误级别。提供的答案和评论确实让我朝着正确的方向前进,因此得到了支持和接受。
不知道为什么我得到了反对票,但我想某人的自我需要通过放下菜鸟来提升 ;-)
感谢您的帮助和回答。
我不确定什么不适合你。能否提供更多信息?
val json =
"""
|{
| "traces":
| [[
| {"matcher": "Matcher1","score": {"assigned": 50,"max": 50}},
| {"matcher": "Matcher2","score": {"assigned": 50,"max": 50}},
| {"matcher": "Matcher3","score": {"assigned": 50,"max": 50}}
| ],
| [
| {"matcher": "Matcher4","score": {"assigned": 50,"max": 50}},
| {"matcher": "Matcher5","score": {"assigned": 50,"max": 50}},
| {"matcher": "Matcher6","score": {"assigned": 50,"max": 50}}
| ]]
| }
""".stripMargin
import net.liftweb.json._
implicit val formats = DefaultFormats
case class Traces (traces: List[List[Trace]])
case class Trace (matcher: String, score: Score)
case class Score(assigned: Int, max: Int)
val result = parse(json).extract[Traces]
上面的代码对我来说没问题。
仍然是 Scala 的菜鸟,我在这里碰壁了。
使用 Scala 2.12.1 和 liftweb 3.0.1
我有json代码
{
"traces":[
[
{
"matcher":"Matcher1",
"score":{
"assigned":50,
"max":50
}
},
{
"matcher":"Matcher2",
"score":{
"assigned":50,
"max":50
}
},
{
"matcher":"Matcher3",
"score":{
"assigned":50,
"max":50
}
}
],
[
{
"matcher":"Matcher4",
"score":{
"assigned":50,
"max":50
}
},
{
"matcher":"Matcher5",
"score":{
"assigned":50,
"max":50
}
},
{
"matcher":"Matcher6",
"score":{
"assigned":50,
"max":50
}
}
]
]
}
我正在尝试使用以下几行将其解析为某些案例分类
case class Traces (traces: List[List[Trace]])
case class Trace (matcher: String, score: Score)
val result = parse(json).extract[Traces]
我知道我的想法有些错误,但我似乎无法找到一种有效解析 json 的方法。
任何能为我指明正确方向的帮助将不胜感激。
[编辑] 我开始明白我确实在这里问错了我的问题。问题是我的示踪剂列表确实得到了 Nil。经过进一步调查,我发现我确实将我的嵌套 class 跟踪器放在了主要 class 的错误级别。提供的答案和评论确实让我朝着正确的方向前进,因此得到了支持和接受。
不知道为什么我得到了反对票,但我想某人的自我需要通过放下菜鸟来提升 ;-)
感谢您的帮助和回答。
我不确定什么不适合你。能否提供更多信息?
val json =
"""
|{
| "traces":
| [[
| {"matcher": "Matcher1","score": {"assigned": 50,"max": 50}},
| {"matcher": "Matcher2","score": {"assigned": 50,"max": 50}},
| {"matcher": "Matcher3","score": {"assigned": 50,"max": 50}}
| ],
| [
| {"matcher": "Matcher4","score": {"assigned": 50,"max": 50}},
| {"matcher": "Matcher5","score": {"assigned": 50,"max": 50}},
| {"matcher": "Matcher6","score": {"assigned": 50,"max": 50}}
| ]]
| }
""".stripMargin
import net.liftweb.json._
implicit val formats = DefaultFormats
case class Traces (traces: List[List[Trace]])
case class Trace (matcher: String, score: Score)
case class Score(assigned: Int, max: Int)
val result = parse(json).extract[Traces]
上面的代码对我来说没问题。