Java 模型的 JSON 字段映射
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/30943675/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
JSON field mapping for Java model
提问by sergionni
JSON which is sent:
发送的 JSON:
{
"Banner": "ABC"
}
Java model:
Java模型:
...
@JsonIgnoreProperties(ignoreUnknown = true)
@JsonSerialize(include = JsonSerialize.Inclusion.NON_NULL)
public class BannerData implements java.io.Serializable {
private static final long serialVersionUID = 5664846645733319592L;
@JsonProperty(value = "Banner")
private String banner;
public String getBanner() {
return banner;
}
public void setBanner(String banner) {
this.banner = banner;
}
}
Controller:
控制器:
@RequestMapping(value = {"/GetBanner"}, method = RequestMethod.POST)
@ResponseBody
public ResponseEntity<String> enrollCustomer(@RequestBody BannerData body, HttpServletRequest request) throws Exception {
...
}
request to /GetBanner
returns:
The request sent by the client was syntactically incorrect.
请求/GetBanner
返回:
客户端发送的请求在语法上不正确。
Works OKwhen json changed to (lowercase naming as is Java field name):
工程确定当JSON变更为(小写字母命名的就是Java字段名):
{
"banner": "ABC"
}
However I need uppercasefield naming in JSON.
Looks like @JsonProperty(value = "Banner")
does not work.
但是我需要JSON 中的大写字段命名。
看起来不起作用。@JsonProperty(value = "Banner")
Is it correct mapping?
它是正确的映射吗?
采纳答案by Arthur Eirich
Maybe you just try to:
也许你只是尝试:
@JsonProperty("Banner")
without 'value ='. I used it in my project and it actually worked as expected.
没有“值=”。我在我的项目中使用了它,它实际上按预期工作。
UPDATE
更新
I just created some test classes to test the bahaviour of your problem. What I have done:
我刚刚创建了一些测试类来测试您的问题的行为。我做了什么:
import org.codehaus.Hymanson.annotate.JsonIgnoreProperties;
import org.codehaus.Hymanson.annotate.JsonProperty;
import org.codehaus.Hymanson.map.annotate.JsonSerialize;
@JsonIgnoreProperties(ignoreUnknown = true)
@JsonSerialize(include = JsonSerialize.Inclusion.NON_NULL)
public class BannerData implements java.io.Serializable {
private static final long serialVersionUID = 5664846645733319592L;
@JsonProperty("Banner")
private String banner;
public String getBanner() {
return banner;
}
public void setBanner(String banner) {
this.banner = banner;
}
}
Another class to read the json:
另一个读取json的类:
import org.codehaus.Hymanson.map.ObjectMapper;
import java.io.File;
import java.io.IOException;
public class BannerReader {
private static final String JSON_PATH = "pathToYourJson";
public BannerData readBanner() throws IOException {
ObjectMapper mapper = new ObjectMapper();
return mapper.readValue(new File(JSON_PATH), BannerData.class);
}
}
And finally the entry point class:
最后是入口点类:
import java.io.IOException;
public class BannerTest {
public static void main(String[] args) throws IOException {
BannerReader reader = new BannerReader();
BannerData bannerData = reader.readBanner();
System.out.println(bannerData.getBanner());
}
}
prints:
印刷:
ABC
Used dependency:
使用的依赖:
<dependency>
<groupId>org.codehaus.Hymanson</groupId>
<artifactId>Hymanson-mapper-asl</artifactId>
<version>1.9.13</version>
</dependency>
json file:
json文件:
{
"Banner": "ABC"
}
UPDATE 2
更新 2
While the above code does not work for you, try adding
虽然上面的代码对您不起作用,请尝试添加
@JsonProperty("Banner")
not only to the private variable, but also to the getter/setter pair, like:
不仅是私有变量,还有 getter/setter 对,比如:
@JsonProperty("Banner")
private String banner;
@JsonProperty("Banner")
public String getBanner() {
return banner;
}
@JsonProperty("Banner")
public void setBanner(String banner) {
this.banner = banner;
}
回答by Ashish Patil
I would suggest below; (If you are using ObjectMapper)
我会在下面建议; (如果您使用的是 ObjectMapper)
once you get your ObjectMapper, you can set PropertyNamingStrategy with PascalCase. this is link for documentation
获得 ObjectMapper 后,您可以使用 PascalCase 设置 PropertyNamingStrategy。这是文档的链接
ObjectMapper mapper = new ObjectMapper();
mapper.setPropertyNamingStrategy(
PropertyNamingStrategy.PascalCaseStrategy);
If you are not using ObjectMapper- then Annotate your BannerData
class with @JsonNaming
like below--
如果您不使用 ObjectMapper- 然后BannerData
使用@JsonNaming
如下注释您的类-
@JsonNaming(PropertyNamingStrategy.LowerCaseWithUnderscoresStrategy.class)
public class BannerData implements java.io.Serializable {
I found out @JsonNaming here in github
我在 github 中发现了 @JsonNaming
回答by Shaun
Try it on the getter
在吸气剂上试试
@JsonProperty(value = "Banner")
public String getBanner() {
EDIT:
编辑:
Show us your Spring config file. Are you really using Hymanson? See http://www.journaldev.com/2552/spring-restful-web-service-example-with-json-Hymanson-and-client-program
向我们展示您的 Spring 配置文件。你真的在用Hyman逊吗?见http://www.journaldev.com/2552/spring-restful-web-service-example-with-json-Hymanson-and-client-program
<!-- Configure to plugin JSON as request and response in method handler -->
<beans:bean class="org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter">
<beans:property name="messageConverters">
<beans:list>
<beans:ref bean="jsonMessageConverter"/>
</beans:list>
</beans:property>
</beans:bean>
<!-- Configure bean to convert JSON to POJO and vice versa -->
<beans:bean id="jsonMessageConverter" class="org.springframework.http.converter.json.MappingHymanson2HttpMessageConverter">
</beans:bean>
EDIT 2
编辑 2
Since you may or may not be using Hymanson... try some other annotations
@XmlElement(name="Banner")
由于您可能会或可能不会使用 Hymanson ... 尝试一些其他注释
@XmlElement(name="Banner")
From GSON perhaps
@SerializedName("Banner")
也许来自 GSON
@SerializedName("Banner")
回答by pkozlov
Have you tried to update your Hymanson from 1.9.13 to 2.5.4 (it has another artifact group id - com.fasterxml.Hymanson )
您是否尝试将 Hymanson 从 1.9.13 更新到 2.5.4(它有另一个工件组 ID - com.fasterxml.Hymanson )
Is this code works correctly for you?
此代码是否适合您?
ObjectMapper mapper = new ObjectMapper();
BannerData bannerData = mapper.readValue("{ \"Banner\": \"ABC\"}", BannerData.class);
Works fine with me (If I replace "Banner" with "banner" - got wrong object with null "banner" field.)
对我来说很好用(如果我用“横幅”替换“横幅” - 用空的“横幅”字段得到错误的对象。)
Also this code can be useful:
此代码也很有用:
System.out.println(mapper.getDeserializationConfig().getPropertyNamingStrategy());
Something can be wrong with property naming strategy - if you use custom one.
属性命名策略可能有问题 - 如果您使用自定义策略。