使用json4s,将JSON反序列化为Scala案例类(不带索引键)的最佳做法是什么?
<p><strong>By subway</strong><br />
Take the Red line and get off at Rho Fiera Milano Expo stop.<br />
web: <a href="file:///C:/Users/lorinim/AppData/Local/Microsoft/Windows/Temporary%20Internet%20Files/Content.Outlook/22RCQI31/atm-mi.it">atm-mi.it</a><br />
download: <a href="http://h-stage.expopage.net/sites/default/files/files/mappametropolitana.pdf">underground map</a></p>
<p> </p>
<p><strong>By car</strong><br />
From the motorways A7-Genoa, A1 Bologna and A4-Turin take ring road west (tangenziale ovest) and go to fieramilano exit.<br />
From the A4-Venice motorway take the Pero-fieramilano exit.<br />
From the motorways A8-Varese and A9-Como at the Milano nord junction take the A4-Venice and go to fieramilano exit.<br />
From Milan follow the road signs to the motorways A8 Varese or A4 Turin and take the fieramilano exit.</p>
<p><strong>AREA C</strong><br />
Restrictive regulations will be imposed on cars entering downtown Milan (Area C - limited traffic area (Ztl).</p>
<p>web: <a href="http://www.comune.milano.it/wps/portal/ist/it">areac.it</a></p>
<p><strong>By taxi</strong><br />
It is possible to reserve a taxi by calling one of the the following numbers:<br />
+39 02.8585 +39 02.6969 +39 02.4040 +39 02.4000 +39 02.5353</p>
<p><strong>By train</strong><br />
Milan exhibition venue is reachable by railway Passante, suburban lines S5 (Varese-Varese-Milano Passante-Treviglio) and S6 (Novara-Milano Passante -Treviglio); S5 and S6 stop directly in Rho Fiera Milano Expo Station.<br />
S5 and S6 lines are accessible from the heart of Milan in the following stations: Certosa, Villapizzone, Lancetti, Porta Garibaldi sotterranea, Repubblica, Porta Venezia, Dateo and Porta Vittoria.<br />
Further information, timetable and tickets purchasing: trenord.it/it/in-fiera.aspx<br />
Several inter-regional trains on the Turin-Novara-Milan line stop at the Rho Fiera Milano Expo station.<br />
A shuttle service connects the trains stations Rho and Rho Fiera Milano Expo.</p>
<p><strong>By plane</strong></p>
<p><em>From LINATE Airport</em><br />
Bus 73 and X73; get off in Piazza San Babila and take the subway, Red line, to Rho Fiera Milano Expo.<br />
During the exhibition days a bus transfer from/to Linate Airport is available:<br />
- departing from the airport, domestic arrivals area, from 8:30 to 13:30 and from 15:00 to 18:00, every hour.<br />
- departing from fieramilano, East Gate bus parking area, from 9:30 to 12:30 and from 14:00 to 19:00, every hour.</p>
<p><em>From MALPENSA Airport</em><br />
Take Malpensa Express train (Terminal 1 railway station) , directed to Milano Centrale Station – alight at Milano Porta Garibaldi and take Suburban railway lines S5 or S6 (13 minutes journey )<br />
Take Malpensa Express train (Terminal 1 railway station) , directed to Milano Cadorna Station (29 minutes journey on non stop trains). From there take the subway, Red line, to Rho Fiera Milano Expo.</p>
<p>Further information, timetable and tickets purchasing: <a href="http://www.trenord.it/">www.trenord.it</a> or <a href="http://www.malpensaexpress.it/">www.malpensaexpress.it/</a></p>
<p><em>From ORIO AL SERIO Airport</em><br />
Direct bus transportation available only during events:<br />
- Orio Shuttle (€ 12.00/one way - € 20.00 round trip): departures from the airport, Arrival area, bus shelter 1, at 8:45, 9:15 and 10:30. Departures from Fiera Milano, East gate bus parking area, at 15:30, 16:30 and 18:00.</p>
<p>web: <a href="http://www.orioshuttle.com/">www.orioshuttle.com</a></p>
<p>Orio bus Express (€ 11.50/one way - € 19.50 round trip):<br />
departures from the airport, Arrivals, bus shelter 3/4, at: 8:30, 10:15 and 11:00. Departures from Fiera Milano at 15:15, 16:00 and 18:00 from East Gate bus parking area.</p>
<p>web: <a href="http://www.autostradale.it/">autostradale.it</a></p>
some.json
{
"1": {
"id": 1,
"year": 2014
},
"2": {
"id": 2,
"year": 2015
},
"3": {
"id": 3,
"year": 2016
}
}
答案 0 :(得分:1)
您应该将json反序列化为相应的scala数据结构。在您的情况下,类型为Map[Int, Foo]
。所以先提取这种类型。帮助片段是:
import org.json4s._
import org.json4s.native.JsonMethods._
implicit lazy val formats = DefaultFormats
val json =
"""
|{
| "1": {
| "id": 1,
| "year": 2014
| },
| "2": {
| "id": 2,
| "year": 2015
| },
| "3": {
| "id": 3,
| "year": 2016
| }
|}
""".stripMargin
case class Foo(id: Int, year: Int)
val ast = parse(json)
val fooMap = ast.extract[Map[Int, Foo]]
结果:
Map(1 -> Foo(1,2014), 2 -> Foo(2,2015), 3 -> Foo(3,2016))