我正在编写一个程序,我需要从json文件中获取一些数据,内容如下所示。
{
"culture": "en-us",
"subscription_key": "myKey",
"description": "myDescription",
"name": "myName",
"appID": "myAppId",
"entities": [
{
"name": "Location"
},
{
"name": "geography"
}
]
}
使用在线工具我为此创建了POJO。它们如下所示。
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;
}
}
实体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;
}
}
我正在使用以下代码打印文件中的值。
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();
}
我得到的输出是
MyAppId
com.config.Entity@2096442d
com.config.Entity@9f70c54
此处不是打印可用值,而是打印Hashcode。请告诉我如何打印这些值。
由于
答案 0 :(得分:1)
只需像这样访问getter方法entity.getName()
,然后使用Entity
代替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();
}
答案 1 :(得分:0)
您尚未定义将“实体”转换为字符串的含义,因此Java将回归到默认的方式(即打印类名和对象ID)。
当您说要“打印可用值”时,您的意思是什么?在这种情况下,值是Entity类型的Java对象,您基本上是打印值。
您可以通过覆盖toString()方法来控制对象的String表示形式。例如,您可以将以下内容添加到Entity类:
@Override
public String toString() {
return "An entity named " + name;
}