Jackson Parser example in android
Jackson parser’s ObjectMapper for reading value and transforms it into a Java object.
ObjectMapper mapper = new ObjectMapper();
Person person1, person2, person3;
//read from an url
person1 = mapper.readValue(new URL("https://api.myjson.com/bins/hoh4j"), Person.class);
//read from a string
String personJsonStr = "{\"firstname\":\"John\",\"lastname\":\"Doe\"}";
person2 = mapper.readValue(personJsonStr, Person.class);
//read from a file
person3 = mapper.readValue(new File("/a/path/to/person.json"), Person.class);
Jackson parser’s ObjectMapper for writing to json string from Java object.
ObjectMapper mapper = new ObjectMapper();
Person person = new Person();
mapper.writeValue(new File("/a/path/to/person.json"), person); // write to file
String jsonStr = mapper.writeValueAsString(person); // write to string
1. Jackson parser dependencies to be included in the gradle file.
compile 'com.fasterxml.jackson.core:jackson-databind:2.8.5' compile 'com.fasterxml.jackson.core:jackson-core:2.8.5' compile 'com.fasterxml.jackson.core:jackson-annotations:2.8.5'
2. Exclude META-INF/LICENSE in the android tag in the gradle file to avoid dup license file error in Android studio
packagingOptions {
exclude 'META-INF/LICENSE'
}
3. Sample json string, https://api.myjson.com/bins/hoh4j
{
"firstname": "John",
"lastname": "Doe"
}
4. Sample model class with Jackson annotations for the above json string.
import java.util.HashMap;
import java.util.Map;
import com.fasterxml.jackson.annotation.JsonAnyGetter;
import com.fasterxml.jackson.annotation.JsonAnySetter;
import com.fasterxml.jackson.annotation.JsonIgnore;
import com.fasterxml.jackson.annotation.JsonInclude;
import com.fasterxml.jackson.annotation.JsonProperty;
import com.fasterxml.jackson.annotation.JsonPropertyOrder;
@JsonInclude(JsonInclude.Include.NON_NULL)
@JsonPropertyOrder({
"firstname",
"lastname"
})
public class Person {
@JsonProperty("firstname")
private String firstname;
@JsonProperty("lastname")
private String lastname;
@JsonIgnore
private Map additionalProperties = new HashMap();
@JsonProperty("firstname")
public String getFirstname() {
return firstname;
}
@JsonProperty("firstname")
public void setFirstname(String firstname) {
this.firstname = firstname;
}
@JsonProperty("lastname")
public String getLastname() {
return lastname;
}
@JsonProperty("lastname")
public void setLastname(String lastname) {
this.lastname = lastname;
}
@JsonAnyGetter
public Map getAdditionalProperties() {
return this.additionalProperties;
}
@JsonAnySetter
public void setAdditionalProperty(String name, Object value) {
this.additionalProperties.put(name, value);
}
}
5. Json string to Java object using Jackson parser
private void jsonToObject() {
final ObjectMapper mapper = new ObjectMapper();
try {
String personJsonStr = "{\"firstname\":\"John\",\"lastname\":\"Doe\"}";
//Person person = mapper.readValue(new File(getFilesDir(), "person.json"), Person.class); // read from file
Person person = mapper.readValue(personJsonStr, Person.class); // read from json string
tvDisplay.setText("json string -> object\n" + person.getFirstname() + " " + person.getLastname());
} catch (IOException e) {
e.printStackTrace();
tvDisplay.setText(e.getMessage());
progressBar.setVisibility(View.GONE);
}
}
6. Json string from an url to Java object using Jackson parser in Android, since there is network involved, the INTERNET use-permission needs to be enabled in the manifest file and it has to run in a background thread.
private void jsonToObjectUrl() {
final ObjectMapper mapper = new ObjectMapper();
final Handler handler = new Handler();
new Thread(new Runnable() {
@Override
public void run() {
try {
Person person = mapper.readValue(new URL("https://api.myjson.com/bins/hoh4j"), Person.class); // read from url
handler.post(new Runnable() {
@Override
public void run() {
tvDisplay.setText("json string -> object\n" + person.getFirstname() + " " + person.getLastname());
}
});
} catch (MalformedURLException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
}).start();
}
7. Java object to Json string using Jackson parser.
private void objectToJson() {
ObjectMapper mapper = new ObjectMapper();
try {
//mapper.writeValue(new File(getFilesDir(), "person.json"), person); // write to file
String jsonStr = mapper.writeValueAsString(person); // write to string
tvDisplay.setText("object -> json string\n" + jsonStr);
} catch (JsonProcessingException e) {
e.printStackTrace();
tvDisplay.setText(e.getMessage());
} catch (IOException e) {
e.printStackTrace();
tvDisplay.setText(e.getMessage());
}
}
Search within Codexpedia
Search the entire web