JSON:未识别字段,未标记为可忽略字段

时间:2021-07-14 18:01:02

I need to convert a certain JSON string to a Java object. I am using Jackson for JSON handling. I have no control over the input JSON (I read from a web service). This is my input JSON:

我需要将某个JSON字符串转换为Java对象。我使用Jackson处理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:

我的包装类基本上是一个容器对象来获取我的学生列表:

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.jackson.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.jackson.map.exc.UnrecognizedPropertyException
    .from(UnrecognizedPropertyException.java:53)

32 个解决方案

#1


713  

You can use Jackson's class-level annotation:

您可以使用Jackson的类级注释:

@JsonIgnoreProperties

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 Jackson's website. If you want to ignore any non declared property, you should write:

它会忽略你在POJO中没有定义的所有属性。当您只是在JSON中寻找几个属性而不想编写整个映射时,这是非常有用的。更多信息在杰克逊的网站。如果你想忽略任何未申报的财产,你应该写:

@JsonIgnoreProperties(ignoreUnknown = true)

#2


308  

You can use

您可以使用

ObjectMapper objectMapper = getObjectMapper();
objectMapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);

It will ignore all the properties that are not declared.

它将忽略所有未声明的属性。

#3


105  

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方法名。

#4


56  

using Jackson 2.6.0, this worked for me:

使用Jackson 2.6.0,这对我很有效:

private static final ObjectMapper objectMapper = 
    new ObjectMapper()
        .configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);

and with setting:

与设置:

@JsonIgnoreProperties(ignoreUnknown = true)

#5


37  

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)注释则没有。

#6


31  

This works better than All please refer to this property.

这比所有的都好,请参考这个属性。

import com.fasterxml.jackson.databind.DeserializationFeature;
import com.fasterxml.jackson.databind.ObjectMapper;

    ObjectMapper objectMapper = new ObjectMapper();
    objectMapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);
    projectVO = objectMapper.readValue(yourjsonstring, Test.class);

#7


30  

it can be achieved 2 ways:

可以通过两种方式实现:

  1. Mark the POJO to ignore unknown properties

    标记POJO以忽略未知属性

    @JsonIgnoreProperties(ignoreUnknown = true)
    
  2. Configure ObjectMapper that serializes/De-serializes the POJO/json as below:

    配置ObjectMapper,它序列化/反序列化POJO/json如下:

    ObjectMapper mapper =new ObjectMapper();            
    // for Jackson version 1.X        
    mapper.configure(DeserializationConfig.Feature.FAIL_ON_UNKNOWN_PROPERTIES, false);
    // for Jackson version 2.X
    mapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false) 
    

#8


26  

If you are using Jackson 2.0

如果您正在使用Jackson 2.0

ObjectMapper mapper = new ObjectMapper();
mapper.disable(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES);

#9


15  

According to the doc you can ignore selected fields or all uknown fields:

根据doc,可以忽略选定的字段或所有uknown字段:

 // to prevent specified fields from being serialized or deserialized
 // (i.e. not include in JSON output; or being set even if they were included)
 @JsonIgnoreProperties({ "internalId", "secretKey" })

 // To ignore any unknown properties in JSON input without exception:
 @JsonIgnoreProperties(ignoreUnknown=true)

#10


14  

It worked for me with the following code:

它对我起作用的代码如下:

ObjectMapper mapper =new ObjectMapper();    
mapper.configure(DeserializationConfig.Feature.FAIL_ON_UNKNOWN_PROPERTIES, false);

#11


10  

Jackson 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".

Jackson在抱怨,因为它在类包装器中找不到一个称为“包装器”的字段。之所以这样做,是因为您的JSON对象有一个名为“包装器”的属性。

I think the fix is to rename your Wrapper class's field to "wrapper" instead of "students".

我认为解决方案是将包装器类的字段重命名为“包装器”,而不是“学生”。

#12


10  

I have tried the below method and it works for such JSON format reading with Jackson. Use the already suggested solution of: annotating getter with @JsonProperty("wrapper")

我尝试过下面的方法,它适用于Jackson的这种JSON格式的读取。使用已建议的解决方案:用@JsonProperty(“包装器”)注释getter

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/jackson-annotations

更多信息请参考https://github.com/fasterxml/jackson -annotation。

Hope this helps

希望这有助于

#13


9  

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.jackson.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对模式转换器执行后者。

#14


8  

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中的属性称为“包装器”,而包装器中的属性称为“包装器”。类被称为“学生”。

So either...

所以要么…

  1. Correct the name of the property in either the class or JSON.
  2. 在类或JSON中修改属性的名称。
  3. Annotate your property variable as per StaxMan's comment.
  4. 根据StaxMan的评论注释您的属性变量。
  5. Annotate the setter (if you have one)
  6. 注释setter(如果有的话)

#15


8  

Annotate the field students as below since there is mismatch in names of json property and java property

如下所示注释字段学生,因为json属性和java属性的名称不匹配

public Class Wrapper {
    @JsonProperty("wrapper")
    private List<Student> students;
    //getters & setters here
}

#16


5  

Your input

您的输入

{"wrapper":[{"id":"13","name":"Fred"}]}

indicates that it is an Object, with a field named "wrapper", which is a Collection of Students. So my recommendation would be,

指示它是一个对象,具有一个名为“包装器”的字段,该字段是学生的集合。所以我的建议是,

Wrapper = mapper.readValue(jsonStr , Wrapper.class);

where Wrapper is defined as

包装器的定义是什么

class Wrapper {
    List<Student> wrapper;
}

#17


4  

What worked for me, was to make the property public. It solved the problem for me.

对我起作用的是,将房产公布于众。它为我解决了问题。

#18


4  

For my part, the only line

对我来说,这是唯一的一条线

@JsonIgnoreProperties(ignoreUnknown = true)

@JsonIgnoreProperties(ignoreUnknown = true)

didn't work too.

没有工作。

Just add

只需添加

@JsonInclude(Include.NON_EMPTY)

Jackson 2.4.0

杰克逊测试盒框

#19


4  

This worked perfectly for me

这对我来说非常有效

objectMapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);

#20


4  

I fixed this problem by simply changing the signatures of my setter and getter methods of my POJO class. All I had to do was change the getObject method to match what the mapper was looking for. In my case I had a getImageUrl originally, but the JSON data had image_url which was throwing the mapper off. I changed both my setter and getters to getImage_url and setImage_url.

通过更改POJO类的setter和getter方法的签名,我解决了这个问题。我所要做的就是更改getObject方法以匹配映射器所查找的内容。在我的例子中,我本来有一个getImageUrl,但是JSON数据有image_url,它将映射器抛出。

Hope this helps.

希望这个有帮助。

#21


3  

Either Change

要么改变

public Class Wrapper {
    private List<Student> students;
    //getters & setters here
}

to

public Class Wrapper {
    private List<Student> wrapper;
    //getters & setters here
}

---- or ----

- - - - - - - - - -

Change your JSON string to

将JSON字符串更改为

{"students":[{"id":"13","name":"Fred"}]}

#22


2  

The POJO should be defined as

POJO应该定义为

Response class

响应类

public class Response {
    private List<Wrapper> wrappers;
    // getter and setter
}

Wrapper class

包装器类

public class Wrapper {
    private String id;
    private String name;
    // getters and setters
}

and mapper to read value

和mapper读取值

Response response = mapper.readValue(jsonStr , Response.class);

#23


2  

set public your class fields not private.

将类字段设置为public而不是private。

public Class Student { 
    public String name;
    public String id;
    //getters & setters for name & id here
}

#24


1  

In my case it was simple: the REST-service JSON Object was updated (a property was added), but the REST-client JSON Object wasn't. As soon as i've updated JSON client object the 'Unrecognized field ...' exception has vanished.

在我的例子中,这很简单:更新了REST-service JSON对象(添加了一个属性),但没有更新REST-client JSON对象。一旦我更新了JSON客户端对象“无法识别的字段……”“异常已经消失了。

#25


1  

This may be a very late response, but just changing the POJO to this should solve the json string provided in the problem (since, the input string is not in your control as you said):

这可能是一个非常晚的响应,但是只要将POJO更改为这个就可以解决问题中提供的json字符串(因为输入字符串不像您说的那样在您的控件中):

public class Wrapper {
    private List<Student> wrapper;
    //getters & setters here
}

#26


1  

Google brought me here and i was surprised to see the answers... all suggested bypassing the error ( which always bites back 4 folds later in developement ) rather than solving it until this gentleman restored by faith in SO!

谷歌把我带到这里,我惊讶地看到了答案……所有人都建议绕过这个错误(在开发过程中,这个错误总是会回缩4倍),而不是解决它,直到这位绅士因信仰如此而恢复原状!

objectMapper.readValue(responseBody, TargetClass.class)

is used to convert a json String to an class object, whats missing is that the TargetClass should have public getter / setters. Same is missing in OP's question snippet too! :)

用于将json字符串转换为类对象,但缺少的是TargetClass应该具有公共getter / setter。OP的问题片段中也缺少相同的内容!:)

via lombok your class as below should work!!

通过lombok您的类如下所示!!

@Data
@Builder
public class TargetClass {
    private String a;
}

#27


1  

The new Firebase Android introduced some huge changes ; below the copy of the doc :

新的Firebase Android带来了一些巨大的变化;以下是文件副本:

[https://firebase.google.com/support/guides/firebase-android] :

[https://firebase.google.com/support/guides/firebase-android]:

Update your Java model objects

更新您的Java模型对象。

As with the 2.x SDK, Firebase Database will automatically convert Java objects that you pass to DatabaseReference.setValue() into JSON and can read JSON into Java objects using DataSnapshot.getValue().

与2。xsdk, Firebase数据库将自动将传递给DatabaseReference.setValue()的Java对象转换为JSON,并可以使用DataSnapshot.getValue()将JSON读入Java对象。

In the new SDK, when reading JSON into a Java object with DataSnapshot.getValue(), unknown properties in the JSON are now ignored by default so you no longer need @JsonIgnoreExtraProperties(ignoreUnknown=true).

在新的SDK中,当使用DataSnapshot.getValue()将JSON读入Java对象时,JSON中的未知属性现在被默认忽略,因此不再需要@JsonIgnoreExtraProperties(ignoreUnknown=true)。

To exclude fields/getters when writing a Java object to JSON, the annotation is now called @Exclude instead of @JsonIgnore.

要在将Java对象写入JSON时排除字段/getter,该注释现在被称为@Exclude,而不是@JsonIgnore。

BEFORE

@JsonIgnoreExtraProperties(ignoreUnknown=true)
public class ChatMessage {
   public String name;
   public String message;
   @JsonIgnore
   public String ignoreThisField;
}

dataSnapshot.getValue(ChatMessage.class)

AFTER

public class ChatMessage {
   public String name;
   public String message;
   @Exclude
   public String ignoreThisField;
}

dataSnapshot.getValue(ChatMessage.class)

If there is an extra property in your JSON that is not in your Java class, you will see this warning in the log files:

如果您的JSON中没有Java类中的额外属性,您将在日志文件中看到这个警告:

W/ClassMapper: No setter/field for ignoreThisProperty found on class com.firebase.migrationguide.ChatMessage

You can get rid of this warning by putting an @IgnoreExtraProperties annotation on your class. If you want Firebase Database to behave as it did in the 2.x SDK and throw an exception if there are unknown properties, you can put a @ThrowOnExtraProperties annotation on your class.

您可以通过在类上添加@IgnoreExtraProperties注释来摆脱这个警告。如果您希望Firebase数据库像2中那样运行。xsdk并抛出异常如果有未知属性,可以在类上添加@ThrowOnExtraProperties注释。

#28


0  

You should just change the field of List from "students" to "wrapper" just the json file and the mapper will look it up.

您只需将列表字段从“students”更改为“wrapper”,只需将json文件,映射器将查找它。

#29


0  

Your json string is not inline with the mapped class. Change the input string

json字符串不与映射类内联。改变输入字符串

String jsonStr = "{\"students\"\:[{\"id\":\"13\",\"name\":\"Fred\"}]}";

Or change your mapped class

或者更改映射类

public class Wrapper {
    private List<Student> wrapper;
    //getters & setters here
}

#30


0  

In my case error came due to following reason

在我的案例中,错误是由于以下原因造成的

  • Initially it was working fine,then i renamed one variable,made the changes in code and it gave me this error.

    最初它运行得很好,然后我重新命名了一个变量,在代码中做了修改,它给了我这个错误。

  • Then i applied jackson ignorant property also but it did not work.

    然后我又申请了杰克逊的无知财产,但它不起作用。

  • Finally after re defining my getters and setters methods according to name of my variable this error was resolved

    最后,在根据变量的名称重新定义getter和setter方法之后,这个错误得到了解决

So make sure to redifine getters and setters also.

所以一定要重拨好getter和setter。

#1


713  

You can use Jackson's class-level annotation:

您可以使用Jackson的类级注释:

@JsonIgnoreProperties

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 Jackson's website. If you want to ignore any non declared property, you should write:

它会忽略你在POJO中没有定义的所有属性。当您只是在JSON中寻找几个属性而不想编写整个映射时,这是非常有用的。更多信息在杰克逊的网站。如果你想忽略任何未申报的财产,你应该写:

@JsonIgnoreProperties(ignoreUnknown = true)

#2


308  

You can use

您可以使用

ObjectMapper objectMapper = getObjectMapper();
objectMapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);

It will ignore all the properties that are not declared.

它将忽略所有未声明的属性。

#3


105  

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方法名。

#4


56  

using Jackson 2.6.0, this worked for me:

使用Jackson 2.6.0,这对我很有效:

private static final ObjectMapper objectMapper = 
    new ObjectMapper()
        .configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);

and with setting:

与设置:

@JsonIgnoreProperties(ignoreUnknown = true)

#5


37  

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)注释则没有。

#6


31  

This works better than All please refer to this property.

这比所有的都好,请参考这个属性。

import com.fasterxml.jackson.databind.DeserializationFeature;
import com.fasterxml.jackson.databind.ObjectMapper;

    ObjectMapper objectMapper = new ObjectMapper();
    objectMapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);
    projectVO = objectMapper.readValue(yourjsonstring, Test.class);

#7


30  

it can be achieved 2 ways:

可以通过两种方式实现:

  1. Mark the POJO to ignore unknown properties

    标记POJO以忽略未知属性

    @JsonIgnoreProperties(ignoreUnknown = true)
    
  2. Configure ObjectMapper that serializes/De-serializes the POJO/json as below:

    配置ObjectMapper,它序列化/反序列化POJO/json如下:

    ObjectMapper mapper =new ObjectMapper();            
    // for Jackson version 1.X        
    mapper.configure(DeserializationConfig.Feature.FAIL_ON_UNKNOWN_PROPERTIES, false);
    // for Jackson version 2.X
    mapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false) 
    

#8


26  

If you are using Jackson 2.0

如果您正在使用Jackson 2.0

ObjectMapper mapper = new ObjectMapper();
mapper.disable(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES);

#9


15  

According to the doc you can ignore selected fields or all uknown fields:

根据doc,可以忽略选定的字段或所有uknown字段:

 // to prevent specified fields from being serialized or deserialized
 // (i.e. not include in JSON output; or being set even if they were included)
 @JsonIgnoreProperties({ "internalId", "secretKey" })

 // To ignore any unknown properties in JSON input without exception:
 @JsonIgnoreProperties(ignoreUnknown=true)

#10


14  

It worked for me with the following code:

它对我起作用的代码如下:

ObjectMapper mapper =new ObjectMapper();    
mapper.configure(DeserializationConfig.Feature.FAIL_ON_UNKNOWN_PROPERTIES, false);

#11


10  

Jackson 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".

Jackson在抱怨,因为它在类包装器中找不到一个称为“包装器”的字段。之所以这样做,是因为您的JSON对象有一个名为“包装器”的属性。

I think the fix is to rename your Wrapper class's field to "wrapper" instead of "students".

我认为解决方案是将包装器类的字段重命名为“包装器”,而不是“学生”。

#12


10  

I have tried the below method and it works for such JSON format reading with Jackson. Use the already suggested solution of: annotating getter with @JsonProperty("wrapper")

我尝试过下面的方法,它适用于Jackson的这种JSON格式的读取。使用已建议的解决方案:用@JsonProperty(“包装器”)注释getter

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/jackson-annotations

更多信息请参考https://github.com/fasterxml/jackson -annotation。

Hope this helps

希望这有助于

#13


9  

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.jackson.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对模式转换器执行后者。

#14


8  

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中的属性称为“包装器”,而包装器中的属性称为“包装器”。类被称为“学生”。

So either...

所以要么…

  1. Correct the name of the property in either the class or JSON.
  2. 在类或JSON中修改属性的名称。
  3. Annotate your property variable as per StaxMan's comment.
  4. 根据StaxMan的评论注释您的属性变量。
  5. Annotate the setter (if you have one)
  6. 注释setter(如果有的话)

#15


8  

Annotate the field students as below since there is mismatch in names of json property and java property

如下所示注释字段学生,因为json属性和java属性的名称不匹配

public Class Wrapper {
    @JsonProperty("wrapper")
    private List<Student> students;
    //getters & setters here
}

#16


5  

Your input

您的输入

{"wrapper":[{"id":"13","name":"Fred"}]}

indicates that it is an Object, with a field named "wrapper", which is a Collection of Students. So my recommendation would be,

指示它是一个对象,具有一个名为“包装器”的字段,该字段是学生的集合。所以我的建议是,

Wrapper = mapper.readValue(jsonStr , Wrapper.class);

where Wrapper is defined as

包装器的定义是什么

class Wrapper {
    List<Student> wrapper;
}

#17


4  

What worked for me, was to make the property public. It solved the problem for me.

对我起作用的是,将房产公布于众。它为我解决了问题。

#18


4  

For my part, the only line

对我来说,这是唯一的一条线

@JsonIgnoreProperties(ignoreUnknown = true)

@JsonIgnoreProperties(ignoreUnknown = true)

didn't work too.

没有工作。

Just add

只需添加

@JsonInclude(Include.NON_EMPTY)

Jackson 2.4.0

杰克逊测试盒框

#19


4  

This worked perfectly for me

这对我来说非常有效

objectMapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);

#20


4  

I fixed this problem by simply changing the signatures of my setter and getter methods of my POJO class. All I had to do was change the getObject method to match what the mapper was looking for. In my case I had a getImageUrl originally, but the JSON data had image_url which was throwing the mapper off. I changed both my setter and getters to getImage_url and setImage_url.

通过更改POJO类的setter和getter方法的签名,我解决了这个问题。我所要做的就是更改getObject方法以匹配映射器所查找的内容。在我的例子中,我本来有一个getImageUrl,但是JSON数据有image_url,它将映射器抛出。

Hope this helps.

希望这个有帮助。

#21


3  

Either Change

要么改变

public Class Wrapper {
    private List<Student> students;
    //getters & setters here
}

to

public Class Wrapper {
    private List<Student> wrapper;
    //getters & setters here
}

---- or ----

- - - - - - - - - -

Change your JSON string to

将JSON字符串更改为

{"students":[{"id":"13","name":"Fred"}]}

#22


2  

The POJO should be defined as

POJO应该定义为

Response class

响应类

public class Response {
    private List<Wrapper> wrappers;
    // getter and setter
}

Wrapper class

包装器类

public class Wrapper {
    private String id;
    private String name;
    // getters and setters
}

and mapper to read value

和mapper读取值

Response response = mapper.readValue(jsonStr , Response.class);

#23


2  

set public your class fields not private.

将类字段设置为public而不是private。

public Class Student { 
    public String name;
    public String id;
    //getters & setters for name & id here
}

#24


1  

In my case it was simple: the REST-service JSON Object was updated (a property was added), but the REST-client JSON Object wasn't. As soon as i've updated JSON client object the 'Unrecognized field ...' exception has vanished.

在我的例子中,这很简单:更新了REST-service JSON对象(添加了一个属性),但没有更新REST-client JSON对象。一旦我更新了JSON客户端对象“无法识别的字段……”“异常已经消失了。

#25


1  

This may be a very late response, but just changing the POJO to this should solve the json string provided in the problem (since, the input string is not in your control as you said):

这可能是一个非常晚的响应,但是只要将POJO更改为这个就可以解决问题中提供的json字符串(因为输入字符串不像您说的那样在您的控件中):

public class Wrapper {
    private List<Student> wrapper;
    //getters & setters here
}

#26


1  

Google brought me here and i was surprised to see the answers... all suggested bypassing the error ( which always bites back 4 folds later in developement ) rather than solving it until this gentleman restored by faith in SO!

谷歌把我带到这里,我惊讶地看到了答案……所有人都建议绕过这个错误(在开发过程中,这个错误总是会回缩4倍),而不是解决它,直到这位绅士因信仰如此而恢复原状!

objectMapper.readValue(responseBody, TargetClass.class)

is used to convert a json String to an class object, whats missing is that the TargetClass should have public getter / setters. Same is missing in OP's question snippet too! :)

用于将json字符串转换为类对象,但缺少的是TargetClass应该具有公共getter / setter。OP的问题片段中也缺少相同的内容!:)

via lombok your class as below should work!!

通过lombok您的类如下所示!!

@Data
@Builder
public class TargetClass {
    private String a;
}

#27


1  

The new Firebase Android introduced some huge changes ; below the copy of the doc :

新的Firebase Android带来了一些巨大的变化;以下是文件副本:

[https://firebase.google.com/support/guides/firebase-android] :

[https://firebase.google.com/support/guides/firebase-android]:

Update your Java model objects

更新您的Java模型对象。

As with the 2.x SDK, Firebase Database will automatically convert Java objects that you pass to DatabaseReference.setValue() into JSON and can read JSON into Java objects using DataSnapshot.getValue().

与2。xsdk, Firebase数据库将自动将传递给DatabaseReference.setValue()的Java对象转换为JSON,并可以使用DataSnapshot.getValue()将JSON读入Java对象。

In the new SDK, when reading JSON into a Java object with DataSnapshot.getValue(), unknown properties in the JSON are now ignored by default so you no longer need @JsonIgnoreExtraProperties(ignoreUnknown=true).

在新的SDK中,当使用DataSnapshot.getValue()将JSON读入Java对象时,JSON中的未知属性现在被默认忽略,因此不再需要@JsonIgnoreExtraProperties(ignoreUnknown=true)。

To exclude fields/getters when writing a Java object to JSON, the annotation is now called @Exclude instead of @JsonIgnore.

要在将Java对象写入JSON时排除字段/getter,该注释现在被称为@Exclude,而不是@JsonIgnore。

BEFORE

@JsonIgnoreExtraProperties(ignoreUnknown=true)
public class ChatMessage {
   public String name;
   public String message;
   @JsonIgnore
   public String ignoreThisField;
}

dataSnapshot.getValue(ChatMessage.class)

AFTER

public class ChatMessage {
   public String name;
   public String message;
   @Exclude
   public String ignoreThisField;
}

dataSnapshot.getValue(ChatMessage.class)

If there is an extra property in your JSON that is not in your Java class, you will see this warning in the log files:

如果您的JSON中没有Java类中的额外属性,您将在日志文件中看到这个警告:

W/ClassMapper: No setter/field for ignoreThisProperty found on class com.firebase.migrationguide.ChatMessage

You can get rid of this warning by putting an @IgnoreExtraProperties annotation on your class. If you want Firebase Database to behave as it did in the 2.x SDK and throw an exception if there are unknown properties, you can put a @ThrowOnExtraProperties annotation on your class.

您可以通过在类上添加@IgnoreExtraProperties注释来摆脱这个警告。如果您希望Firebase数据库像2中那样运行。xsdk并抛出异常如果有未知属性,可以在类上添加@ThrowOnExtraProperties注释。

#28


0  

You should just change the field of List from "students" to "wrapper" just the json file and the mapper will look it up.

您只需将列表字段从“students”更改为“wrapper”,只需将json文件,映射器将查找它。

#29


0  

Your json string is not inline with the mapped class. Change the input string

json字符串不与映射类内联。改变输入字符串

String jsonStr = "{\"students\"\:[{\"id\":\"13\",\"name\":\"Fred\"}]}";

Or change your mapped class

或者更改映射类

public class Wrapper {
    private List<Student> wrapper;
    //getters & setters here
}

#30


0  

In my case error came due to following reason

在我的案例中,错误是由于以下原因造成的

  • Initially it was working fine,then i renamed one variable,made the changes in code and it gave me this error.

    最初它运行得很好,然后我重新命名了一个变量,在代码中做了修改,它给了我这个错误。

  • Then i applied jackson ignorant property also but it did not work.

    然后我又申请了杰克逊的无知财产,但它不起作用。

  • Finally after re defining my getters and setters methods according to name of my variable this error was resolved

    最后,在根据变量的名称重新定义getter和setter方法之后,这个错误得到了解决

So make sure to redifine getters and setters also.

所以一定要重拨好getter和setter。