杰克逊没有填充所有财产 [英] Jackson not populating all properties
问题描述
我正在研究一个简单的例子,使用 Jackson库
将 json
字符串转换回 Java对象
但我看到我的java对象上只设置了几个属性而不是所有属性。
I am working on a simple example using Jackson library
to convert a json
string back to Java object
but I see only few properties are being set on my java object instead of all properties.
这是我的代码:
import java.io.BufferedReader;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import org.codehaus.jackson.map.ObjectMapper;
public class JsonTest {
public static void main(String[] args) throws FileNotFoundException, IOException {
StringBuffer buffer = new StringBuffer();
String data = "";
BufferedReader reader = null;
try {
reader = new BufferedReader(new FileReader("path-to-sample.json"));
while ((data = reader.readLine()) != null) {
buffer.append(data);
}
} finally {
if (reader != null) {
reader.close();
}
}
System.out.println(buffer.toString());
ObjectMapper mapper = new ObjectMapper();
Sample obj = mapper.readValue(buffer.toString(), Sample.class);
System.out.println(obj);
}
}
Sample.java程序如下所示:
The Sample.java program looks like this:
import org.codehaus.jackson.annotate.JsonIgnoreProperties;
import com.fasterxml.jackson.annotation.JsonProperty;
@JsonIgnoreProperties(ignoreUnknown = true)
public class Sample {
@JsonProperty("prop_1")
private String prop1;
private String prop2;
@JsonProperty("prop_3")
private String prop3;
private String prop4;
// Setters & Getters for the properties.
@Override
public String toString() {
return "Sample [prop1=" + prop1 + ", prop2=" + prop2 + ", prop3="
+ prop3 + ", prop4=" + prop4 + "]";
}
}
在我的文件中输入json字符串是:
Input json string in my file is :
{
"prop_1": "1",
"prop2": "2",
"prop_3": "3",
"prop4": "4"
}
该程序的输出为:
Sample [prop1=null, prop2=2, prop3=null, prop4=4]
根据我的程序, prop1
和 prop3
不应为null。我不清楚我错在哪里。
As per my program the prop1
and prop3
should not be null. I am not clear where I made mistake.
更新:
如果我删除 @JsonProperty
注释然后我得到例外:
If I remove the @JsonProperty
annotation then I am getting the exception as :
Exception in thread "main" org.codehaus.jackson.map.exc.UnrecognizedPropertyException: Unrecognized field "prop_1" (Class Sample), not marked as ignorable
这是我的pom.xml文件依赖项:
This is my pom.xml file dependencies:
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-core</artifactId>
<version>2.6.0</version>
</dependency>
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-annotations</artifactId>
<version>2.6.0</version>
</dependency>
<dependency>
<groupId>org.codehaus.jackson</groupId>
<artifactId>jackson-mapper-asl</artifactId>
<version>1.9.13</version>
</dependency>
推荐答案
你在评论中说过你正在使用杰克逊在版本2.5.4,但你从 org.codehaus中导入
包。这意味着,此类来自版本1.9.13(或旧版本)。 ObjectMapper
类
You said in your comment, that you're using Jackson in version "2.5.4", but you're importing the ObjectMapper
class from the org.codehaus
package. This means, that this class is from version 1.9.13 (or from an older version).
如果我使用<$ c $混合版本,我可以重现您的问题c> ObjectMapper 和 JsonIgnoreProperties
版本1.9.13( org.codehaus
)和 JsonProperty
版本2.6.0( com.fasterxml
)。
I can reproduce your problem if I mix the versions using ObjectMapper
and JsonIgnoreProperties
from version 1.9.13 (org.codehaus
) and JsonProperty
from version 2.6.0 (com.fasterxml
).
输出:
示例[prop1 = null,prop2 = 2,prop3 = null,prop4 = 4]
Sample [prop1=null, prop2=2, prop3=null, prop4=4]
如果我只使用版本1.9.13或2.6.0,那么结果还可以:
If I only use version 1.9.13 or 2.6.0, then the result is ok:
样本[prop1 = 1,prop2 = 2,prop3 = 3,prop4 = 4]
Sample [prop1=1, prop2=2, prop3=3, prop4=4]
(对于两者而言)
因此我建议您确保不要混用使用过的库,我建议使用最新版本,该版本来自 FasterXML 。
但是使用的版本由你决定。
So I recommend to make sure that you don't mix the used libraries and I recommend to use the newest version, which is from FasterXML. But the used version is up to you.
你可以从这里下载jar文件:
You can download the jar file from here:
com。 fasterxml.jackson(core)v2.6.0
关于你的评论:
@OldCurmudgeon,感谢您的回复。将字段更改为公共并未解决问题。我删除了@JsonProperty注释,然后将setter方法更改为setProp_1& setProp_3,它的工作原理。这是否意味着@JsonProperty注释存在问题?
@OldCurmudgeon, Thanks for responding. Changing the fields to public has not fixed the issue. I have removed the @JsonProperty annotation and then changed the setter methods to setProp_1 & setProp_3, it worked. So does it mean that there is an issue with @JsonProperty annotation?
是的,你有(或希望有:P)问题使用该注释:它来自不同的Jackson版本。
Yes, you have (or hopefully had :P) a problem with that annotation: it was from a different Jackson version.
关于您的编辑:
指向来自maven存储库中的fasterXML的Jackson lib有一个很大的优势:它显示了你应该下载哪个lib来与你的项目中的Jackson一起工作。
The link to the Jackson lib from fasterXML in the maven repository has one big advantage: it shows you which lib you should download to work with Jackson in your project.
你需要:
- Jackson Databind (也有
ObjectMapper
类) - Jackson Core
- Jackson Annotations
- Jackson Databind (which also has the
ObjectMapper
class) - Jackson Core
- Jackson Annotations
这篇关于杰克逊没有填充所有财产的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!