如何使用scala将json文件转换为列表

时间:2018-06-28 16:47:24

标签: scala

我有以下json输入 sample.json

[{"level": 1, "firstFile": "one", "secondFile": "first"},
 {"level": 1, "firstFile": "two", "secondFile": "sec"},
 {"level": 2, "firstFile": "three", "secondFile": "third"}]

我希望我的结果应该是:

val first= List(List("one","two"),List("three"))
val second= List(List("first","sec"),List("third"))

build.sbt:

name := "Test"
version := "1.0"
scalaVersion := "2.11.0"

libraryDependencies ++= Seq("org.apache.spark" % "spark-sql_2.11" % "2.0.0" % "provided")
libraryDependencies += "com.typesafe" % "config" % "1.2.0"
libraryDependencies ++= Seq("org.slf4j" % "slf4j-api" % "1.7.5",
                            "org.clapper" %% "grizzled-slf4j" % "1.3.1")
libraryDependencies += "ch.qos.logback" % "logback-core" % "1.2.3"
libraryDependencies += "ch.qos.logback" % "logback-classic" % "1.2.3" % "test"
libraryDependencies += "com.databricks" % "spark-avro_2.11" % "4.0.0"
libraryDependencies += "io.circe" %% "circe-config" % "0.4.1"               
retrieveManaged := true

2 个答案:

答案 0 :(得分:1)

通常,您使用Jackson或Circe之类的库将JSON转换为case类。

val json = """[{"level": 1, "firstFile": "one", "secondFile": "first"},
             | {"level": 1, "firstFile": "one", "secondFile": "first"},
             | {"level": 2, "firstFile": "two", "secondFile": "sec"},
             | {"level": 2, "firstFile": "two", "secondFile": "sec"}]""".stripMargin

case class My2FileThing(level: Int, firstFile: String, secondFile: String)

val parsed: List[My2FileThing] = {
  // TODO parse json using some library; there are many available

  List(
    My2FileThing(1, "one", "first"),
    My2FileThing(1, "one", "first"),
    My2FileThing(2, "two", "sec"),
    My2FileThing(2, "two", "sec")
  )
}

您提供的json是一维List,其中包含上述四个项。

我想,要将数据转换为您要的两个列表,可以执行此操作。您尚未告诉我们进行转换的逻辑。

val firstLineChandraWants: List[List[String]] =
  parsed.map(_.firstFile).groupBy(identity).values.toList
val secondLineChandraWants: List[List[String]] =
  parsed.map(_.secondFile).groupBy(identity).values.toList

答案 1 :(得分:0)

使用一些库将Json字符串转换为Scala对象/类型。

有关更多信息:How to convert JSON to a type in Scala

使用提升的示例:

import net.liftweb.json._
implicit val formats = DefaultFormats // Brings in default date formats etc.
val json = """
[{"level": 1, "firstFile": "one", "secondFile": "first"},
 {"level": 1, "firstFile": "two", "secondFile": "sec"},
 {"level": 2, "firstFile": "three", "secondFile": "third"}]
"""
case class ListSubType(level: Int, firstFile: String, secondFile: String)

val parsed: List[ListSubType] =  parse(json).extract[List[ListSubType]]
val desired: List[List[String]] = parsed.map(a => List(a.firstFile, a.secondFile))

// will produce: List(List("one", "first"), List("two", "sec"), List("three", "third"))

我知道您在不同的变量中询问了不同的(根列表的元素),但是使用索引或遍历列表比使用不同的变量更好。实际上,列表的长度是不确定的,因此如何将不同的项目分配给不同的变量。 例子=>

/**
* Using index
*/
val first:Option[String] = desired.get(0) // will return Option[String]
val second:Option[String] = desired.get(1) // will return Option[String]

for (items <- desired) {
   println(items)
}

使用此链接获取所需的构建工具/软件包管理器的导入代码。

https://search.maven.org/#artifactdetails%7Cnet.liftweb%7Clift-json%7C2.0%7Cjar