杰克逊克服下划线支持驼峰式 [英] Jackson overcoming underscores in favor of camel-case

查看:31
本文介绍了杰克逊克服下划线支持驼峰式的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我从互联网上检索了一个 JSON 字符串;像我见过的大多数 JSON 一样,它包含由下划线分隔的长键.本质上,我的目标是将 JSON 反序列化为 java 对象,但我不在 java 代码中使用下划线.

I retrieve a JSON string from internet; like most JSON I've seen it includes long keys that are separated by underscores. Essentially, my goal is to deserialize JSON into java-objects, but I don't use underscores in java-code.

例如,我可能有一个带有 firstName 字段的 User 类在驼峰式大小写中,同时我需要以某种方式告诉 Jackson 映射 first_name 从 JSON 到 firstName 类字段的键.可能吗?

For instance, I might have a User class with firstName field in camel-case, simultaneously I need somehow to tell Jackson to map first_name key from JSON to firstName class field. Is it possible?

class User{
    protected String firstName;
    protected String getFirstName(){return firstName;}
}

推荐答案

您应该在要更改默认名称映射的字段上使用 @JsonProperty.

You should use the @JsonProperty on the field you want to change the default name mapping.

class User{
    @JsonProperty("first_name")
    protected String firstName;
    protected String getFirstName(){return firstName;}
}

更多信息:API

这篇关于杰克逊克服下划线支持驼峰式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆