使用scala提取嵌套的JSON元素(Extract nested JSON element using scala)

我在Scala中有以下代码。 我的目标是在不知道密钥的数量和深度的情况下提取密钥的值。


import org.json4s.jackson.JsonMethods._ import org.json4s.{DefaultFormats, JField, JObject, JString, JValue} object jsonLift { implicit val formats = DefaultFormats val js = parse( """ { "url": "imap.yahoo.com", "credentials": { "username": "myusername", "password": "mypassword" }, "some key":{ "other key": { "username": "hello" } } } """) def getElem(elem: String, json:JValue) = for { JObject(child) <- json JField(elem, JString(value)) <- child // this does not return the expected result // JField("username", JString(value)) <- child // this returns the expected result } yield value def main(args: Array[String]) { val result = getElem("username", js) println(result) } }

上面代码的结果是List(imap.yahoo.com, myusername, mypassword, hello) ,这不是我所期待的。 我的预期结果是List(myusername, hello) 。

但是,如果我直接在方法getElem更改变量elem ,使用我感兴趣的键(作为字符串)(例如:“username”),我得到了预期的结果: List(myusername, hello) ,它们都是键“username”的值。

如何通过调用方法getElem并将JSON键的名称作为参数来获取预期的值列表? 例如: getElem("JSON key", json)

谢谢!

I have the following code in Scala. My goal is to extract the value(s) of a key without knowing how many and how deep they are.


import org.json4s.jackson.JsonMethods._ import org.json4s.{DefaultFormats, JField, JObject, JString, JValue} object jsonLift { implicit val formats = DefaultFormats val js = parse( """ { "url": "imap.yahoo.com", "credentials": { "username": "myusername", "password": "mypassword" }, "some key":{ "other key": { "username": "hello" } } } """) def getElem(elem: String, json:JValue) = for { JObject(child) <- json JField(elem, JString(value)) <- child // this does not return the expected result // JField("username", JString(value)) <- child // this returns the expected result } yield value def main(args: Array[String]) { val result = getElem("username", js) println(result) } }

The result of the above code is List(imap.yahoo.com, myusername, mypassword, hello) which is not what I am expecting. My expected result is List(myusername, hello).

However, if I change the variable elem, directly inside the method getElem, with the key (as string) that I am interested in (Eg: "username") I get the expected result: List(myusername, hello) which are all the values of the key "username".

How can I get the expected list of value(s) by calling the method getElem with the name of the JSON key as argument? Eg: getElem("JSON key", json)

Thank you!

最满意答案

将getElem更改为

def getElem(elem: String, json:JValue) = for { JObject(child) <- json JField(`elem`, JString(value)) <- child } yield value

如果没有RHS上elem周围的反引号type JField = (String, JValue)的第一个元素绑定到新名称elem并将方法参数elem隐藏

Change getElem to

def getElem(elem: String, json:JValue) = for { JObject(child) <- json JField(`elem`, JString(value)) <- child } yield value

Without the backticks around elem on the RHS, you are binding the first element of type JField = (String, JValue) to a new name elem and shadowing the method parameter elem

更多推荐