如何在Scala中将典型的Json文件格式打印到Hashmap(Map)中?

时间:2017-02-20 05:28:31

标签: java json scala jackson

我编写了一个Scala代码,我正在尝试使用fasterxml.jackson包读取Json文件并且能够成功读取。在代码行号28中我试图将Json字符串转换为Hashmap(scala),它是已转换,但我正在尝试将输出转换为系统格式,如下所示。

Json文件

{   "RedDef": [
  {
    "RedDeIn": 2,
    "RedTypC": "XY",
    "RedTypP": ".XY.*$",
    "MinNoOfAt": 19,
    "ExpNoOfAt": 19,
    "RedLen":117,
    "attributes": [
      {
        "AttId": 1,
        "AttNa": "Pro Ty",
        "AttMaxLen": 1,
        "DataTy": {
          "DataName": "AN"
        }
      },
      {
        "AttId": 2,
        "AttNa": "Pro Cod",
        "AttMaxLen": 6,
        "DataTy": {
          "DataName": "AN"
        }
      },
      {
        "AttId": 3,
        "AttNa": "REQ CODE",
        "AttMaxLen": 7,
        "DataTy": {
          "DataName": "AN"
        }
      }
    ]
  },
  {
    "RedDeIn": 3,
    "RedTypC": "JK",
    "RedTypP": "JK.*$",
    "MinNoOfAt": 10,
    "ExpNoOfAt": 10,
    "attributes": [
      {
        "AttId": 1,
        "AttNa": "Log Si Ty",
        "AttMaxLen": 1,
        "DataTy": {
          "DataName": "A"
        }
      },
      {
        "AttId": 2,
        "AttNa": "Log Si Cod",
        "AttMaxLen": 6,
        "DataTy": {
          "DataName": "AN"
        }
      },
      {
        "AttId": 3,
        "AttNa": "Ex Qu Cod",
        "AttMaxLen": 7,
        "DataTy": {
          "DataName": "AN"
        }
      }
    ]
  },

  {
    "RedDeIn": 4,
    "RedTypC": "FK",
    "RedTypP": "FK.*$",
    "MinNoOfAt": 33,
    "ExpNoOfAt": 33,
    "RedLen":117,
    "attributes": [
      {
        "AttId": 1,
        "AttNa": "Pro Ty",
        "AttMaxLen": 1,
        "DataTy": {
          "DataName": "AN"
        }
      },
      {
        "AttId": 2,
        "AttNa": "Pro Cod",
        "AttMaxLen": 6,
        "DataTy": {
          "DataName": "AN"
        }
      },
      {
        "AttId": 3,
        "AttNa": "REQ CDE",
        "AttMaxLen": 7,
        "DataTy": {
          "DataName": "AN"
        }
      },
      {
        "AttId": 4,
        "AttNa": "RED Ty",
        "AttMaxLen": 2,
        "DataTy": {
          "DataName": "AN"
        }
      }
    ]
  }
]
}

代码

    import com.fasterxml.jackson.databind.ObjectMapper
    import com.fasterxml.jackson.module.scala.DefaultScalaModule
    import com.fasterxml.jackson.module.scala.experimental.ScalaObjectMapper
    import scala.io.Source


    object Json_Parser_Jackson1  {

      case class RecordDefinitions(RedDeIn: Int
                                   ,RedTypC: String
                                   ,RedTypP: String
                                   ,MinNoOfAt: Int
                                   ,ExpNoOfAt: Int
                                   ,RedLen: Int
                                   ,attributes: Seq[Attributes])
      case class Attributes(AttId: Int
                            ,AttNa: String
                            ,AttMaxLen: Int
                            ,dateFormat: Any
                            ,DataTy: DataType)
      case class DataType(DataTyName: String)
      case class Definition(RedDef : Seq[RecordDefinitions])
      def main(args: Array[String]): Unit = {
        val fileContent = Source.fromFile("C:\\kkk\\xxx\\ideaProject\\jsonparser\\src\\main\\resources\\Json_file.json", "UTF-8").getLines.mkString

        val mapper = new ObjectMapper() with ScalaObjectMapper
        mapper.registerModule(DefaultScalaModule)
        val testWithMap  = mapper.readValue[Map[_, _]](fileContent)
        println(testWithMap)

      }}

输出来自我的代码,如下所示

Map(RedDef -> List(Map(RedTypP -> ^.{14}XY.*$, RedTypC -> XY, ExpNoOfAt -> 19, attributes -> List(Map(AttId -> 1, AttNa -> Pro Ty, AttMaxLen -> 1, DataTy -> Map(DataTyName -> AN)), Map(AttId -> 2, AttNa -> Pro Cod, AttMaxLen -> 6, DataTy -> Map(DataTyName -> AN)), Map(AttId -> 3, AttNa -> REQ CODE, AttMaxLen -> 7, DataTy -> Map(DataTyName -> AN))), RedLen -> 117, RedDeIn -> 2, MinNoOfAt -> 19), Map(RedTypP -> ^.{14}JK.*$, RedTypC -> JK, ExpNoOfAt -> 10, attributes -> List(Map(AttId -> 1, AttNa -> Log Si Ty, AttMaxLen -> 1, DataTy -> Map(DataTyName -> A)), Map(AttId -> 2, AttNa -> Log Si Cod, AttMaxLen -> 6, DataTy -> Map(DataTyName -> AN)), Map(AttId -> 3, AttNa -> Ex Qu Cod, AttMaxLen -> 7, DataTy -> Map(DataTyName -> AN))), RedDeIn -> 3, MinNoOfAt -> 10), Map(RedTypP -> ^.{14}FK.*$, RedTypC -> FK, ExpNoOfAt -> 33, attributes -> List(Map(AttId -> 1, AttNa -> Pro Ty, AttMaxLen -> 1, DataTy -> Map(DataTyName -> AN)), Map(AttId -> 2, AttNa -> Pro Cod, AttMaxLen -> 6, DataTy -> Map(DataTyName -> AN)), Map(AttId -> 3, AttNa -> REQ CDE, AttMaxLen -> 7, DataTy -> Map(DataTyName -> AN)), Map(AttId -> 4, AttNa -> RED Ty, AttMaxLen -> 2, DataTy -> Map(DataTyName -> AN))), RedLen -> 117, RedDeIn -> 4, MinNoOfAt -> 33)))

我想要的输出如下

RedTypP = ^.{14}XY.*$
RedTypC = XY
ExpNoOfAt = 19
AttId = 1
AttNa = Pro Ty
AttMaxLen =1
DataTyName = AN
AttId = 2
AttNa = Pro Cod
AttMaxLen = 6
DataName = AN
AttId = 3
AttNa = REQ CODE
AttMaxLen = 7
DataTyName = AN
RedLen = 117
RedDeIn = 2
MinNoOfAt =19 

1 个答案:

答案 0 :(得分:0)

您可能需要自己美化地图,如下所示,(我使用了回忆)

val awesomeMap = Map("k1" -> "v1",
  "k2" -> 2,
  "k3" -> 10.28,
  "k4" -> List(Map("k4.k1" -> "v3", "k4.k2" -> List(Map("k4.k3" -> "v4")))))

printInKeyValueFormat(awesomeMap)

def printInKeyValueFormat(givenAMap: Map[String, Any]): Unit = {
  givenAMap foreach {
    case (k: String, v: String) => println(k + "=" + v)
    case (k: String, v: Int) => println(k + "=" + v)
    case (k: String, v: Long) => println(k + "=" + v)
    case (k: String, v: Double) => println(k + "=" + v)
    case (k: String, v: List[Map[String, Any]]) => v.foreach(x => printInKeyValueFormat(x))
  }
}

输出

k1=v1
k2=2
k3=10.28
k4.k1=v3
k4.k3=v4

如果需要,您可以添加更多案例。