如何迭代 org.json4s.JsonAST.JValue,它是一个 JSON 对象数组,以分别处理 Scala 中的每个对象?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/34898951/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-22 07:57:35  来源:igfitidea点击:

How to iterate org.json4s.JsonAST.JValue which is an array of JSON objects to separately work on each object in Scala?

jsonscalajson4s

提问by GKVM

I have a sample array:

我有一个示例数组:

[{
    "abc":"1",
    "de":"1"
},
{
    "fgh":"2",
    "ij":"4"
}]

which is a org.json4s.JsonAST.JValue.

这是一个 org.json4s.JsonAST.JValue。

How is it possible to iterate over each object inside the array, to operate on each object separately?

如何遍历数组内的每个对象,分别对每个对象进行操作?

回答by ixguza

The following is your json.

以下是你的json。

scala> json
res2: org.json4s.JValue = JArray(List(JObject(List((abc,JString(1)), (de,JString(1)))),
        JObject(List((fgh,JString(2)), (ij,JString(4))))))

There are several ways.

有几种方法。

  1. use forsyntax

    for {
      JArray(objList) <- json
      JObject(obj) <- objList
    } {
      // do something
      val kvList = for ((key, JString(value)) <- obj) yield (key, value)
      println("obj : " + kvList.mkString(","))
    }
    
  2. convert to scala.collectionobject

    val list = json.values.asInstanceOf[List[Map[String, String]]]
    //=> list: List[Map[String,String]] = List(Map(abc -> 1, de -> 1), Map(fgh -> 2, ij -> 4))
    

    or

    implicit val formats = DefaultFormats
    val list = json.extract[List[Map[String,String]]]
    //=> list: List[Map[String,String]] = List(Map(abc -> 1, de -> 1), Map(fgh -> 2, ij -> 4))
    

    and do something.

    for (obj <- list) println("obj : " + obj.toList.mkString(","))
    
  1. 使用for语法

    for {
      JArray(objList) <- json
      JObject(obj) <- objList
    } {
      // do something
      val kvList = for ((key, JString(value)) <- obj) yield (key, value)
      println("obj : " + kvList.mkString(","))
    }
    
  2. 转换为scala.collection对象

    val list = json.values.asInstanceOf[List[Map[String, String]]]
    //=> list: List[Map[String,String]] = List(Map(abc -> 1, de -> 1), Map(fgh -> 2, ij -> 4))
    

    或者

    implicit val formats = DefaultFormats
    val list = json.extract[List[Map[String,String]]]
    //=> list: List[Map[String,String]] = List(Map(abc -> 1, de -> 1), Map(fgh -> 2, ij -> 4))
    

    并做某事。

    for (obj <- list) println("obj : " + obj.toList.mkString(","))
    

Both outputs are

两个输出都是

obj : (abc,1),(de,1)
obj : (fgh,2),(ij,4)

The document of json4s is here.

json4s 的文档在这里

回答by Yuri Schimke

You should be able to either cast to JArray

您应该能够转换为 JArray

val myArray = myVal.asInstanceOf[JArray]
myArray.arr // the array

or preferably use a scala match so you can confirm the type is correct.

或者最好使用 scala 匹配,以便您确认类型正确。