Reputation: 3351
I'm writing a program where I need to get some data from a json file and the content is as below.
{
"culture": "en-us",
"subscription_key": "myKey",
"description": "myDescription",
"name": "myName",
"appID": "myAppId",
"entities": [
{
"name": "Location"
},
{
"name": "geography"
}
]
}
using an online tool I've created the POJOs for the same. and they are as below.
ConfigDetails Pojo
package com.config;
import java.util.HashMap;
import java.util.List;
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({
"culture",
"subscription_key",
"description",
"name",
"appID",
"entities"
})
public class ConfigDetails {
@JsonProperty("culture")
private String culture;
@JsonProperty("subscription_key")
private String subscriptionKey;
@JsonProperty("description")
private String description;
@JsonProperty("name")
private String name;
@JsonProperty("appID")
private String appID;
@JsonProperty("entities")
private List<Entity> entities = null;
@JsonProperty("culture")
public String getCulture() {
return culture;
}
@JsonProperty("culture")
public void setCulture(String culture) {
this.culture = culture;
}
@JsonProperty("subscription_key")
public String getSubscriptionKey() {
return subscriptionKey;
}
@JsonProperty("subscription_key")
public void setSubscriptionKey(String subscriptionKey) {
this.subscriptionKey = subscriptionKey;
}
@JsonProperty("description")
public String getDescription() {
return description;
}
@JsonProperty("description")
public void setDescription(String description) {
this.description = description;
}
@JsonProperty("name")
public String getName() {
return name;
}
@JsonProperty("name")
public void setName(String name) {
this.name = name;
}
@JsonProperty("appID")
public String getAppID() {
return appID;
}
@JsonProperty("appID")
public void setAppID(String appID) {
this.appID = appID;
}
@JsonProperty("entities")
public List<Entity> getEntities() {
return entities;
}
@JsonProperty("entities")
public void setEntities(List<Entity> entities) {
this.entities = entities;
}
}
Entity POJO
package com.config;
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({
"name"
})
public class Entity {
@JsonProperty("name")
private String name;
@JsonProperty("name")
public String getName() {
return name;
}
@JsonProperty("name")
public void setName(String name) {
this.name = name;
}
}
and I'm using the below code to print the values from the file.
MainClass obj = new MainClass();
ObjectMapper mapper = new ObjectMapper();
try {
// Convert JSON string from file to Object
ConfigDetails details = mapper.readValue(new File("properties.json"), ConfigDetails.class);
System.out.println(details.getAppID());
List entities = details.getEntities();
for (Object entity : entities) {
System.out.println(entity.toString());
}
} catch (JsonGenerationException e) {
e.printStackTrace();
} catch (JsonMappingException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
The output that I'm getting is
MyAppId
com.config.Entity@2096442d
com.config.Entity@9f70c54
here instead of printing the value available, it is printing Hashcode. please let me know how can I print the values.
Thanks
Upvotes: 0
Views: 1219
Reputation: 389
You haven't defined what it means to convert an "Entity" to a String, so Java is falling back to its default way of doing this (which is to print the class name and an object ID).
What do you mean when you say you want it to "print the value available"? In this case the values are Java objects of type Entity, and you essentially are printing the values.
You can control what the String representation of an object is by overriding the toString() method. For example, you could add the following to the Entity class:
@Override
public String toString() {
return "An entity named " + name;
}
Upvotes: 0
Reputation: 2820
Just access the getter method entity.getName()
like this and use Entity
instead of Object
:
MainClass obj = new MainClass();
ObjectMapper mapper = new ObjectMapper();
try {
// Convert JSON string from file to Object
ConfigDetails details = mapper.readValue(new File("properties.json"), ConfigDetails.class);
System.out.println(details.getAppID());
List entities = details.getEntities();
for (Entity entity : entities) {
System.out.println(entity.getName());
}
} catch (JsonGenerationException e) {
e.printStackTrace();
} catch (JsonMappingException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
Upvotes: 1