如何在Scala中将映射转换为Json
我的地图如下:如何在Scala中将映射转换为Json,json,scala,Json,Scala,我的地图如下: Map( Columbus Herzog -> Map( 0 -> Map(source -> Las Cruces Crawford Arpt (LRU), amount -> 5000.0, points -> 500, date_time -> Sat Feb 13 02:18:14 IST 2016, class -> First, destination -> San Luis Arpt (IPI)), 1 -&
Map(
Columbus Herzog ->
Map(
0 -> Map(source -> Las Cruces Crawford Arpt (LRU), amount -> 5000.0, points -> 500, date_time -> Sat Feb 13 02:18:14 IST 2016, class -> First, destination -> San Luis Arpt (IPI)),
1 -> Map(source -> Las Cruces Crawford Arpt (LRU), amount -> 5000.0, points -> 500, date_time -> Fri Jul 22 01:52:19 IST 2016, class -> First, destination -> San Luis Arpt (IPI)),
2 -> Map(source -> Las Cruces Crawford Arpt (LRU), amount -> 5000.0, points -> 500, date_time -> Wed Jan 06 06:24:50 IST 2016, class -> First, destination -> San Luis Arpt (IPI)),
3 -> Map(source -> Las Cruces Crawford Arpt (LRU), amount -> 5000.0, points -> 500, date_time -> Tue May 17 17:12:16 IST 2016, class -> First, destination -> San Luis Arpt (IPI))
)
)
{"Columbus Herzog":
[{"source":"Las Cruces Crawford Arpt (LRU)","amount":"5000.0","points":"500","date_time":"Sat Feb 13 02:18:14 IST 2016","class":"First","destination":"San Luis Arpt (IPI)"}],
[{"source":"Las Cruces Crawford Arpt (LRU)","amount":"5000.0","points":"500","date_time":"Fri Jul 22 01:52:19 IST 2016","class":"First","destination":"San Luis Arpt (IPI)"}],
[{"source":"Las Cruces Crawford Arpt (LRU)","amount":"5000.0","points":"500","date_time":"Wed Jan 06 06:24:50 IST 2016","class":"First","destination":"San Luis Arpt (IPI)"}],
[{"source":"Las Cruces Crawford Arpt (LRU)","amount":"5000.0","points":"500","date_time":"Tue May 17 17:12:16 IST 2016","class":"First","destination":"San Luis Arpt (IPI)"}]
}
我希望JSON数组如下所示:
Map(
Columbus Herzog ->
Map(
0 -> Map(source -> Las Cruces Crawford Arpt (LRU), amount -> 5000.0, points -> 500, date_time -> Sat Feb 13 02:18:14 IST 2016, class -> First, destination -> San Luis Arpt (IPI)),
1 -> Map(source -> Las Cruces Crawford Arpt (LRU), amount -> 5000.0, points -> 500, date_time -> Fri Jul 22 01:52:19 IST 2016, class -> First, destination -> San Luis Arpt (IPI)),
2 -> Map(source -> Las Cruces Crawford Arpt (LRU), amount -> 5000.0, points -> 500, date_time -> Wed Jan 06 06:24:50 IST 2016, class -> First, destination -> San Luis Arpt (IPI)),
3 -> Map(source -> Las Cruces Crawford Arpt (LRU), amount -> 5000.0, points -> 500, date_time -> Tue May 17 17:12:16 IST 2016, class -> First, destination -> San Luis Arpt (IPI))
)
)
{"Columbus Herzog":
[{"source":"Las Cruces Crawford Arpt (LRU)","amount":"5000.0","points":"500","date_time":"Sat Feb 13 02:18:14 IST 2016","class":"First","destination":"San Luis Arpt (IPI)"}],
[{"source":"Las Cruces Crawford Arpt (LRU)","amount":"5000.0","points":"500","date_time":"Fri Jul 22 01:52:19 IST 2016","class":"First","destination":"San Luis Arpt (IPI)"}],
[{"source":"Las Cruces Crawford Arpt (LRU)","amount":"5000.0","points":"500","date_time":"Wed Jan 06 06:24:50 IST 2016","class":"First","destination":"San Luis Arpt (IPI)"}],
[{"source":"Las Cruces Crawford Arpt (LRU)","amount":"5000.0","points":"500","date_time":"Tue May 17 17:12:16 IST 2016","class":"First","destination":"San Luis Arpt (IPI)"}]
}
请给我一些建议
提前谢谢。关于
net.liftweb.json.DefaultFormats
import net.liftweb.json.JsonAST
import net.liftweb.json.JsonDSL._
import net.liftweb.json.Printer.{compact,pretty}
object LiftJsonWithCollections extends App {
val json = List(1, 2, 3)
println(compact(JsonAST.render(json)))
val map = Map("1" -> "one", "2" -> "two")
println(compact(JsonAST.render(map)))
}
和输出:
[1,2,3]
{"1":"one","2":"two"}
只需将
Map
的内容替换为own即可。这可能有助于您了解更多关于JSONI的信息,但“分解”会给出类似not found的错误。@ChetanNakum,此示例使用外部库。请确保您将其添加到构建中。您好,谢谢您的回复,但当我尝试添加maven时,出现了类似“加载JsonAST时出错,Scala签名JsonAST的版本错误,预期为:5.0找到:JsonAST.class中的4.1”这样的错误“我的用户位于maven:net.liftweb lift json 2.0-M5之下,我的映射如下:val map=map(“Jerrod Lindgren”->Map(0->Map(“来源”->“克林顿市(CWI)”,“金额”->3000.0,“点数”->300,“日期时间”->“2015年10月16日星期五11:06:03 IST”,“课程”->“商务”,“目的地”->“南京Arpt(NKG)”)@Chetan Nakum您将有一个嵌套的地图。怎么了?