在新结构中转换Json

时间:2016-11-08 19:10:36

标签: arrays json scala

您好我在scala中有一个Web服务,我希望它能够回答ajax请求的特定数据 那么如何在scala中转换这个Json:

 [
  {
    "Quantita": 6,
    "Citta": "BARI",
    "GENERE": "Avventura"
  },

  {
    "Quantita": 30,
    "Citta": "BARI",
    "GENERE": "Storia"
  },
  {
    "Quantita": 6,
    "Citta": "MODUGNO",
    "GENERE": "Avventura"
  },

  {
    "Quantita": 6,
    "Citta": "MODUGNO",
    "GENERE": "Storia"
  },
  {
    "Quantita": 8,
    "Citta": "MODUGNO",
    "GENERE": "Avventura"
  }]

在这样的数组数组中:

 [
['Bari','Avventura',6],
    ['Bari','Storia',30],
    ['Modugno','Avventura',6],
    ['Modugno','Giallo',6],
    ['Modugno','Storia',6],
    ['Avventura','Bari',6],
    ['Avventura','Modugno',6],
    ['Storia','Bari',30],
    ['Storia','Modugno',6]
]

请帮帮我:)。

1 个答案:

答案 0 :(得分:0)

Json4s库

看起来你是Stackoverflow的新手。这是开始的例子。

build.sbt

中创建一个sbt项目

build.sbt

name := "foobar"

version := "1.0"

scalaVersion := "2.11.8"

libraryDependencies ++= Seq("org.json4s" %% "json4s-native" % "3.4.2")
src文件夹中的

Main.scala

import org.json4s._
import org.json4s.native.JsonMethods._

object Main {

  def convert(jsonString: String): String = {
    val parsedJson = parse(jsonString)
    parsedJson.children.map {
      case JObject(list) =>
        val map = list.toMap
        val JString(citta) = map("Citta")
        val JString(genere) = map("GENERE")
        val JInt(quantita) = map("Quantita")
        List(citta, genere, quantita).map(str => s"'$str'").mkString("[", ",", "]\n")
      case _ => List.empty[String]
    }.mkString("[", ",", "]")
  }

  def main(args: Array[String]): Unit = {
    println(convert(
      """
        | [
        |  {
        |    "Quantita": 6,
        |    "Citta": "BARI",
        |    "GENERE": "Avventura"
        |  },
        |
        |  {
        |    "Quantita": 30,
        |    "Citta": "BARI",
        |    "GENERE": "Storia"
        |  },
        |  {
        |    "Quantita": 6,
        |    "Citta": "MODUGNO",
        |    "GENERE": "Avventura"
        |  },
        |
        |  {
        |    "Quantita": 6,
        |    "Citta": "MODUGNO",
        |    "GENERE": "Storia"
        |  },
        |  {
        |    "Quantita": 8,
        |    "Citta": "MODUGNO",
        |    "GENERE": "Avventura"
        |  }]
        |
      """.stripMargin))
  }
}

运行项目执行sbt run并查看控制台上的输出。

注意:根据您的要求格式化您的输出,并照顾驼峰案例。

输出:

[['BARI','Avventura','6']
,['BARI','Storia','30']
,['MODUGNO','Avventura','6']
,['MODUGNO','Storia','6']
,['MODUGNO','Avventura','8']
]

播放Json

build.sbt

name := "foobar"

version := "1.0"

scalaVersion := "2.11.8"

resolvers += "Typesafe repository" at "http://repo.typesafe.com/typesafe/releases/"

libraryDependencies ++= Seq("com.typesafe.play" %% "play-json" % "2.5.4")

Main.scala

import play.api.libs.json._

object Main {

  case class Record(quantita: Int, citta: String, genere: String)

  object Record {
    import play.api.libs.functional.syntax._
    implicit val recordReads: Reads[Record] = (
      (JsPath \ "Quantita").read[Int] and
        (JsPath \ "Citta").read[String] and
        (JsPath \ "GENERE").read[String]
      )(Record.apply _)
  }


  def main(args: Array[String]): Unit = {
    val parsedJson =
    Json.parse(
      """
        |[
        |  {
        |    "Quantita": 6,
        |    "Citta": "BARI",
        |    "GENERE": "Avventura"
        |  },
        |
        |  {
        |    "Quantita": 30,
        |    "Citta": "BARI",
        |    "GENERE": "Storia"
        |  },
        |  {
        |    "Quantita": 6,
        |    "Citta": "MODUGNO",
        |    "GENERE": "Avventura"
        |  },
        |
        |  {
        |    "Quantita": 6,
        |    "Citta": "MODUGNO",
        |    "GENERE": "Storia"
        |  },
        |  {
        |    "Quantita": 8,
        |    "Citta": "MODUGNO",
        |    "GENERE": "Avventura"
        |  }]
        |
      """.stripMargin)
    parsedJson.validate[List[Record]] match {
      case JsSuccess(value, _) =>
        value.map { record =>
          s"""[${record.citta},${record.genere},${record.quantita}]\n"""
        }.mkString("[", ",", "]")
      case JsError(_) => println("")
    }
  }
}