gpt4 book ai didi

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

转载 作者:太空宇宙 更新时间:2023-11-04 11:46:04 24 4
gpt4 key购买 nike

我编写了一个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)

}}

输出来 self 的代码,如下所示

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

最佳答案

您可能需要自己美化 map ,如下所示,(我正在使用recusion)

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

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

关于java - 如何在 Scala 中将典型的 Json 文件格式打印到 Hashmap(Map) 中?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42337092/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com