2016-11-08 29 views
-1

你好,我有Scala中的一個Web服務,我想這與特定的數據回答一個Ajax請求 所以,我怎麼能在斯卡拉轉換這個JSON:轉換一個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] 
] 

請幫我:)

+0

我不知道怎樣才能做到這一點。我需要一些幫助或解決方案 – Mozzer

+0

什麼是JSON庫? – Barry

+0

請提供一個可重現的例子。 –

回答

0

Json4s庫

看起來你是新來的Stackoverflow。這裏是開始的例子。

創建SBT項目,並在build.sbt

build.sbt

name := "foobar" 

version := "1.0" 

scalaVersion := "2.11.8" 

libraryDependencies ++= Seq("org.json4s" %% "json4s-native" % "3.4.2") 

Main.scala在src文件夾

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("") 
    } 
    } 
} 
+0

謝謝!我會嘗試明天。我可以看到你使用json4s庫。我用戲劇框架。也許我可以使這個轉換也玩... – Mozzer

+0

@Mozzer更新與播放json以及 – pamu

+0

請問,你有一個播放解決方案,我會嘗試明天? – Mozzer