访问对象Scala中的属性 [英] Accessing attributes in objects Scala
问题描述
我需要从json中获取的对象类型的示例可以在以下示例(src)中找到:
An example of the sort of objects I need to grab from my json can be found in the following example(src):
{
"test": {
"attra": "2017-10-12T11:17:52.971Z",
"attrb": "2017-10-12T11:20:58.374Z"
},
"dummyCheck": false,
"type": "object",
"ruleOne": {
"default": 2557
},
"ruleTwo": {
"default": 2557
}
}
从上面的例子中我想访问ruleOne下的默认值。
我已经尝试过以下几种不同的事情,但我似乎在苦苦挣扎。我可以抓住像dummyCheck这样的值。键入我需要去的地方的最佳方法是什么?
如何获取以下值的示例:
From the example above I want to access the default value under "ruleOne". I've tried messing about with several different things below but I seem to be struggling. I can grab values like "dummyCheck" ok. What's the best way to key into where I need to go? Example of how I am trying to get the value below:
import org.json4s._
import org.json4s.native.JsonMethods._
import org.json4s.DefaultFormats
implicit val formats = DefaultFormats
val test = parse(src)
println((test \ "ruleOne.default").extract[Integer])
编辑:
进一步扩展上述内容:
To further extend what is above:
def extractData(data: java.io.File) = {
val json = parse(data)
val result = (json \ "ruleOne" \ "default").extract[Int]
result
}
如果我将上述内容扩展为通过传入调用的函数:
If I was to extend the above into a function that is called by passing in:
extractData(src)
这只会给我RuleOne.default ..是否有一种方法可以扩展它,以便我可以动态地将多个字符串参数传递给parse(如splat)
That would only ever give me RuleOne.default.. is there a way I could extend it so that I could dynamically pass it multiple string arguments to parse (like a splat)
def extractData(data: java.io.File, path: String*) = {
val json = parse(data)
val result = (json \ path: _*).extract[Int]
result
}
因此消耗它就像
extractData(src, "ruleOne", "default")
推荐答案
这适用于json4s-jackson%3.6.0-M2
,但它应该与 native
后端完全相同。
This here works with "json4s-jackson" % "3.6.0-M2"
, but it should work in exactly the same way with native
backend.
val src = """
|{
| "test": {
| "attra": "2017-10-12T11:17:52.971Z",
| "attrb": "2017-10-12T11:20:58.374Z"
| },
| "dummyCheck": false,
| "type": "object",
| "ruleOne": {
| "default": 2557
| },
| "ruleTwo": {
| "default": 2557
| }
|}""".stripMargin
import org.json4s._
import org.json4s.jackson.JsonMethods._
import org.json4s.DefaultFormats
implicit val formats = DefaultFormats
val test = parse(src)
println((test \ "ruleOne" \ "default").extract[Int])
输出:
2557
使其与本地$ c一起使用$ c>,只需替换
import org.json4s.jackson.JsonMethods._
by
import org.json4s.native.JsonMethods._
并确保您拥有正确的依赖关系。
and make sure that you have the right dependencies.
编辑
这是 vararg
将字符串参数转换为路径的方法:
Here is a vararg
method that transforms string parameters into a path:
def extract(json: JValue, path: String*): Int = {
path.foldLeft(json)(_ \ _).extract[Int]
}
有了这个,你现在可以这样做:
With this, you can now do:
println(extract(test, "ruleOne", "default"))
println(extract(test, "ruleTwo", "default"))
请注意,它接受 JValue
,而不是文件
,因为带有 File
的版本会不必要地测试,而 JValue
-version可以用解析后的字符串常量进行测试。
Note that it accepts a JValue
, not a File
, because the version with File
would be unnecessarily painful to test, whereas JValue
-version can be tested with parsed string constants.
这篇关于访问对象Scala中的属性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!