如何使用 argonaut 将此 JSON 数组转换为实际数组?
How do I turn this JSON array into an actual array with argonaut?
我把这个 JSON 放在一个单独的文件中:
{
"TestModel" : [
{"query" : "select * from ci_lmaggregation.elementstest where hash_aggregation_key = 0 ALLOW FILTERING "},
{"query" : "select * from ci_lmaggregation.elementstest where hash_aggregation_key = 1 ALLOW FILTERING "},
{"query" : "select * from ci_lmaggregation.elementstest where hash_aggregation_key = 2 ALLOW FILTERING "}
]
}
}
我正在尝试按照他们的快速入门进行操作,但它甚至找不到“decodeOption”或“Parse.parse”或其他任何内容。我很困惑?
val input_file = "[path]/FeatureMapping.json"
val json_content = scala.io.Source.fromFile(input_file).mkString
//it can't find decodeOption
val json_data = json_content.decodeOption[List[Person]].getOrElse(Nil)
//or Parse.parse (which should work given this http://argonaut.io/doc/parsing/)
val jdata = Parse.parse(json_content)
所以我不太确定如何完成我想做的事情。我试过这样做:
Parse JSON array using Scala Argonaut
以及他们网站上的入门指南,但似乎都没有像此处那样处理数组大小写的示例。此外,SO post 中的代码示例显示为不存在,所以我也很困惑。
我只想要一个查询数组作为文件中的字符串,为什么没有像 python 中那样的简单方法来执行此操作?
使用the Dijon library。现在,使用 Scala 来处理无模式 JSON 是一种安全有效的方法。
添加依赖:
libraryDependency += "me.vican.jorge" %% "dijon" % "0.5.0"
导入 Scala 的动态类型特性和库包:
import scala.language.dynamics._
import dijon._
然后解析 select 所需的值 JSON:
val json = parse(
"""{
| "TestModel" : [
| {"query" : "select * from ci_lmaggregation.elementstest where hash_aggregation_key = 0 ALLOW FILTERING "},
| {"query" : "select * from ci_lmaggregation.elementstest where hash_aggregation_key = 1 ALLOW FILTERING "},
| {"query" : "select * from ci_lmaggregation.elementstest where hash_aggregation_key = 2 ALLOW FILTERING "}
| ]
|}""".stripMargin
)
assert(json.TestModel.toSeq.map(_.query) == Seq(
"select * from ci_lmaggregation.elementstest where hash_aggregation_key = 0 ALLOW FILTERING ",
"select * from ci_lmaggregation.elementstest where hash_aggregation_key = 1 ALLOW FILTERING ",
"select * from ci_lmaggregation.elementstest where hash_aggregation_key = 2 ALLOW FILTERING "
))
我把这个 JSON 放在一个单独的文件中:
{
"TestModel" : [
{"query" : "select * from ci_lmaggregation.elementstest where hash_aggregation_key = 0 ALLOW FILTERING "},
{"query" : "select * from ci_lmaggregation.elementstest where hash_aggregation_key = 1 ALLOW FILTERING "},
{"query" : "select * from ci_lmaggregation.elementstest where hash_aggregation_key = 2 ALLOW FILTERING "}
]
}
}
我正在尝试按照他们的快速入门进行操作,但它甚至找不到“decodeOption”或“Parse.parse”或其他任何内容。我很困惑?
val input_file = "[path]/FeatureMapping.json"
val json_content = scala.io.Source.fromFile(input_file).mkString
//it can't find decodeOption
val json_data = json_content.decodeOption[List[Person]].getOrElse(Nil)
//or Parse.parse (which should work given this http://argonaut.io/doc/parsing/)
val jdata = Parse.parse(json_content)
所以我不太确定如何完成我想做的事情。我试过这样做: Parse JSON array using Scala Argonaut
以及他们网站上的入门指南,但似乎都没有像此处那样处理数组大小写的示例。此外,SO post 中的代码示例显示为不存在,所以我也很困惑。
我只想要一个查询数组作为文件中的字符串,为什么没有像 python 中那样的简单方法来执行此操作?
使用the Dijon library。现在,使用 Scala 来处理无模式 JSON 是一种安全有效的方法。
添加依赖:
libraryDependency += "me.vican.jorge" %% "dijon" % "0.5.0"
导入 Scala 的动态类型特性和库包:
import scala.language.dynamics._
import dijon._
然后解析 select 所需的值 JSON:
val json = parse(
"""{
| "TestModel" : [
| {"query" : "select * from ci_lmaggregation.elementstest where hash_aggregation_key = 0 ALLOW FILTERING "},
| {"query" : "select * from ci_lmaggregation.elementstest where hash_aggregation_key = 1 ALLOW FILTERING "},
| {"query" : "select * from ci_lmaggregation.elementstest where hash_aggregation_key = 2 ALLOW FILTERING "}
| ]
|}""".stripMargin
)
assert(json.TestModel.toSeq.map(_.query) == Seq(
"select * from ci_lmaggregation.elementstest where hash_aggregation_key = 0 ALLOW FILTERING ",
"select * from ci_lmaggregation.elementstest where hash_aggregation_key = 1 ALLOW FILTERING ",
"select * from ci_lmaggregation.elementstest where hash_aggregation_key = 2 ALLOW FILTERING "
))