1
我使用Scala的Jackson 1.9.1將對象編組爲JSON。我的編組的代碼如下所示:如何用Java Jackson重命名JSON的根密鑰?
val mapper = new ObjectMapper()
mapper.configure(SerializationConfig.Feature.WRAP_ROOT_VALUE, true)
mapper.setPropertyNamingStrategy(new PropertyNamingStrategy.LowerCaseWithUnderscoresStrategy())
val introspectorPair = new AnnotationIntrospector.Pair(
new JacksonAnnotationIntrospector(),
new JaxbAnnotationIntrospector()
)
mapper.getSerializationConfig().withAnnotationIntrospector(introspectorPair)
val writer = mapper.defaultPrettyPrintingWriter
writer.writeValueAsString(this)
一個典型的JSON本步驟是這個樣子的:
{
"SalesOrder" : {
"id" : "3187e7d0-f84f-11e0-be50-0800200c9a66",
"total_paid" : 8.99,
"created_at" : "2011-05-14T00:00:00.000+0300",
"updated_at" : "2011-05-14T00:00:00.000+0300"
}
}
我的問題是:如何從「SalesOrder」重命名根密鑰更多的JavaScript - 友好的「sales_order」?在我的類定義上面添加一個JsonProperty
覆蓋不起作用 - 大概是因爲根密鑰不是嚴格的屬性(因此setPropertyNamingStrategy()
也沒有被應用)?
任何指導如何實現這個感激的接收!
謝謝@StaxMan,這很好。 (我用'@ XmlRootElement'選項去了)。 –