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.
Here is my code:
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);
}
}
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 + "]";
}
}
Input json string in my file is :
{
"prop_1": "1",
"prop2": "2",
"prop_3": "3",
"prop4": "4"
}
The output of this program is :
Sample [prop1=null, prop2=2, prop3=null, prop4=4]
As per my program the prop1
and prop3
should not be null. I am not clear where I made mistake.
Update:
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
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>
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).
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
).
Output:
Sample [prop1=null, prop2=2, prop3=null, prop4=4]
If I only use version 1.9.13 or 2.6.0, then the result is ok:
Sample [prop1=1, prop2=2, prop3=3, prop4=4]
(for both)
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.
You can download the jar file from here:
Btw about your comment:
@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?
Yes, you have (or hopefully had :P) a problem with that annotation: it was from a different Jackson version.
About your edit:
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.
You need:
ObjectMapper
class)
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.