gpt4 book ai didi

java - 使用 Jackson 打印 json 数组

转载 作者:行者123 更新时间:2023-12-02 13:37:27 24 4
gpt4 key购买 nike

我正在编写一个程序,需要从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

这里不是打印可用的值,而是打印哈希码。请让我知道如何打印这些值。

谢谢

最佳答案

只需像这样访问 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();
}

关于java - 使用 Jackson 打印 json 数组,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42912411/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com