Java Jackson 与 JSON:无法识别的字段,未标记为可忽略
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/4486787/
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
Hymanson with JSON: Unrecognized field, not marked as ignorable
提问by jshree
I need to convert a certain JSON string to a Java object. I am using Hymanson for JSON handling. I have no control over the input JSON (I read from a web service). This is my input JSON:
我需要将某个 JSON 字符串转换为 Java 对象。我正在使用 Hymanson 进行 JSON 处理。我无法控制输入 JSON(我从 Web 服务读取)。这是我的输入 JSON:
{"wrapper":[{"id":"13","name":"Fred"}]}
Here is a simplified use case:
这是一个简化的用例:
private void tryReading() {
String jsonStr = "{\"wrapper\"\:[{\"id\":\"13\",\"name\":\"Fred\"}]}";
ObjectMapper mapper = new ObjectMapper();
Wrapper wrapper = null;
try {
wrapper = mapper.readValue(jsonStr , Wrapper.class);
} catch (Exception e) {
e.printStackTrace();
}
System.out.println("wrapper = " + wrapper);
}
My entity class is:
我的实体类是:
public Class Student {
private String name;
private String id;
//getters & setters for name & id here
}
My Wrapper class is basically a container object to get my list of students:
我的 Wrapper 类基本上是一个容器对象,用于获取我的学生列表:
public Class Wrapper {
private List<Student> students;
//getters & setters here
}
I keep getting this error and "wrapper" returns null
. I am not sure what's missing. Can someone help please?
我不断收到此错误,并且“包装器”返回null
。我不确定缺少什么。有人可以帮忙吗?
org.codehaus.Hymanson.map.exc.UnrecognizedPropertyException:
Unrecognized field "wrapper" (Class Wrapper), not marked as ignorable
at [Source: java.io.StringReader@1198891; line: 1, column: 13]
(through reference chain: Wrapper["wrapper"])
at org.codehaus.Hymanson.map.exc.UnrecognizedPropertyException
.from(UnrecognizedPropertyException.java:53)
回答by Jim Ferrans
Hymanson is complaining because it can't find a field in your class Wrapper that's called "wrapper". It's doing this because your JSON object has a property called "wrapper".
Hymanson 抱怨是因为它在您的类 Wrapper 中找不到一个名为“wrapper”的字段。这样做是因为您的 JSON 对象有一个名为“包装器”的属性。
I think the fix is to rename your Wrapper class's field to "wrapper" instead of "students".
我认为解决方法是将 Wrapper 类的字段重命名为“wrapper”而不是“students”。
回答by StaxMan
The first answer is almost correct, but what is needed is to change getter method, NOT field -- field is private (and not auto-detected); further, getters have precedence over fields if both are visible.(There are ways to make private fields visible, too, but if you want to have getter there's not much point)
第一个答案几乎是正确的,但需要的是更改 getter 方法,而不是字段——字段是私有的(并且不会自动检测到);此外,如果两者都可见,则 getter 优先于字段。(也有一些方法可以使私有字段可见,但如果您想要 getter,则没有多大意义)
So getter should either be named getWrapper()
, or annotated with:
所以 getter 应该被命名为getWrapper()
,或者注释为:
@JsonProperty("wrapper")
If you prefer getter method name as is.
如果您更喜欢 getter 方法名称。
回答by Ariel Kogan
You can use Hymanson's class-level annotation:
您可以使用 Hymanson 的类级注释:
import com.fasterxml.Hymanson.annotation.JsonIgnoreProperties
@JsonIgnoreProperties
class { ...?}
It will ignore every property you haven't defined in your POJO. Very useful when you are just looking for a couple of properties in the JSON and don't want to write the whole mapping. More info at Hymanson's website. If you want to ignore any non declared property, you should write:
它将忽略您尚未在 POJO 中定义的每个属性。当您只是在 JSON 中查找几个属性并且不想编写整个映射时非常有用。Hyman逊网站上的更多信息。如果你想忽略任何未声明的属性,你应该写:
@JsonIgnoreProperties(ignoreUnknown = true)
回答by TedTrippin
As no one else has mentioned, thought I would...
正如没有人提到的那样,我以为我会......
Problem is your property in your JSON is called "wrapper" and your property in Wrapper.class is called "students".
问题是您在 JSON 中的财产被称为“包装器”,而您在 Wrapper.class 中的财产被称为“学生”。
So either...
所以要么...
- Correct the name of the property in either the class or JSON.
- Annotate your property variable as per StaxMan's comment.
- Annotate the setter (if you have one)
- 更正类或 JSON 中的属性名称。
- 根据 StaxMan 的评论注释您的属性变量。
- 注释二传手(如果你有)
回答by mytwocentsads
I have tried the below method and it works for such JSON format reading with Hymanson.
Use the already suggested solution of: annotating getter with @JsonProperty("wrapper")
我已经尝试了以下方法,它适用于使用 Hymanson 读取此类 JSON 格式。使用已经建议的解决方案:注释 getter@JsonProperty("wrapper")
Your wrapper class
你的包装类
public Class Wrapper{
private List<Student> students;
//getters & setters here
}
My Suggestion of wrapper class
我对包装类的建议
public Class Wrapper{
private StudentHelper students;
//getters & setters here
// Annotate getter
@JsonProperty("wrapper")
StudentHelper getStudents() {
return students;
}
}
public class StudentHelper {
@JsonProperty("Student")
public List<Student> students;
//CTOR, getters and setters
//NOTE: If students is private annotate getter with the annotation @JsonProperty("Student")
}
This would however give you the output of the format:
但是,这将为您提供格式的输出:
{"wrapper":{"student":[{"id":13,"name":Fred}]}}
Also for more information refer to https://github.com/FasterXML/Hymanson-annotations
另请参阅https://github.com/FasterXML/Hymanson-annotations了解更多信息
Hope this helps
希望这可以帮助
回答by Suresh
You can use
您可以使用
ObjectMapper objectMapper = getObjectMapper();
objectMapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);
It will ignore all the properties that are not declared.
它将忽略所有未声明的属性。
回答by George Papatheodorou
This solution is generic when reading json streams and need to get only some fields while fields not mapped correctly in your Domain Classes can be ignored:
此解决方案在读取 json 流时是通用的,并且只需要获取一些字段,而域类中未正确映射的字段可以忽略:
import org.codehaus.Hymanson.annotate.JsonIgnoreProperties;
@JsonIgnoreProperties(ignoreUnknown = true)
A detailed solution would be to use a tool such as jsonschema2pojo to autogenerate the required Domain Classes such as Student from the Schema of the json Response. You can do the latter by any online json to schema converter.
一个详细的解决方案是使用诸如 jsonschema2pojo 之类的工具从 json 响应的架构中自动生成所需的域类,例如 Student。您可以通过任何在线 json 到模式转换器来完成后者。
回答by Felix Kimutua
This just perfectly worked for me
这对我来说非常有效
ObjectMapper objectMapper = new ObjectMapper();
objectMapper.configure(
DeserializationConfig.Feature.FAIL_ON_UNKNOWN_PROPERTIES, false);
@JsonIgnoreProperties(ignoreUnknown = true)
annotation did not.
@JsonIgnoreProperties(ignoreUnknown = true)
注释没有。
回答by Karan
This works better than All please refer to this property.
这比 All 效果更好,请参阅此属性。
import com.fasterxml.Hymanson.databind.DeserializationFeature;
import com.fasterxml.Hymanson.databind.ObjectMapper;
ObjectMapper objectMapper = new ObjectMapper();
objectMapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);
projectVO = objectMapper.readValue(yourjsonstring, Test.class);
回答by Aalkhodiry
If you are using Hymanson 2.0
如果您使用的是 Hymanson 2.0
ObjectMapper mapper = new ObjectMapper();
mapper.disable(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES);