java - 如何按字段打印类对象的内容?

标签 java json jackson pojo

我有一个 POJO 类,Location,用于将 JSON 文件映射到使用 Jackson 的类。当前的实现可以通过调用 Location 的 toString() 打印出类中的每个 Location 对象,但我想知道如何打印,例如,仅打印 id = "2"的位置,即 name="Desert"

目前,我使用这样的 toString 方法来打印 Location 的所有内容:

public String toString() {
        return "Location [location=" + Arrays.toString(location) + ", id=" + id
                + ", description=" + description + ", weight=" + weight
                + ", name=" + name + ", exit=" + Arrays.toString(exit)
                +"]";
    }

有谁知道如何根据字段 ID 打印 Location 对象中的特定位置?

这是我调用 toString() 时存储在 Location 类中的示例:

http://hastebin.com/eruxateduz.vhdl

Location 对象中 Locations 之一的示例:

[Location [location=null, id=1, description=You are in the city of Tiberius. You see a long street with high buildings and a castle.You see an exit to the south., weight=100, name=Tiberius, exit=[Exit [title=Desert, direction=South]]]

这是我用来将 JSON 字段映射到类的 POJO 位置类:

public class Location {

    private Location[] location;

    private int id;

    private String description;

    private String weight;

    private String name;

    private Exit[] exit;

    private boolean visited = false;
    private boolean goalLocation;
    private int approximateDistanceFromGoal = 0;
    private Location parent;




    public Location[] getLocation() {
        return location;
    }

    public void setLocation(Location[] location) {
        this.location = location;
    }


    public int getId() {
        return id;
    }

    public void setId(int id) {
        this.id = id;
    }

    public String getDescription ()
    {
        return description;
    }

    public void setDescription (String description)
    {
        this.description = description;
    }


    public String getWeight() {
        return weight;
    }

    public void setWeight(String weight) {
        this.weight = weight;
    }

    public String getName ()
    {
        return name;
    }

    public void setName (String name)
    {
        this.name = name;
    }

    public Exit[] getExit() {
        return exit;
    }

    public void setExit(Exit[] exit) {
        this.exit = exit;
    }


    public boolean isVisited() {
        return visited;
    }

    public void setVisited(boolean visited) {
        this.visited = visited;
    }

    public boolean isGoalLocation() {
        return goalLocation;
    }

    public void setGoalLocation(boolean goalLocation) {
        this.goalLocation = goalLocation;
    }

    public int getApproximateDistanceFromGoal() {
        return approximateDistanceFromGoal;
    }

    public void setApproximateDistanceFromGoal(int approximateDistanceFromGoal) {
        this.approximateDistanceFromGoal = approximateDistanceFromGoal;
    }

    public Location getParent() {
        return parent;
    }

    public void setParent(Location parent) {
        this.parent = parent;
    }

    @Override
    public String toString() {
        return "Location [location=" + Arrays.toString(location) + ", id=" + id
                + ", description=" + description + ", weight=" + weight
                + ", name=" + name + ", exit=" + Arrays.toString(exit)
                +"]";
    }



}

最佳答案

<dependency>
    <groupId>org.apache.commons</groupId>
    <artifactId>commons-collections4</artifactId>
    <version>4.0</version>
</dependency>

除非您想自己做,否则您需要上述依赖项来定义谓词。

public class Location {
   private int id;
   // more stuff here

   private Predicate<Integer> filter;
     public Location() {
          this.filter = TruePredicate.INSTANCE; 
     }
     public Location(int idFilter) {
             this.filter = new EqualPrediate(idFilter);
     }

     public String toString() {
       StringBuilder buffer = new StringBuilder();
          if(filter.apply(this.id)) { 
             buffer.append("Location [location=" + Arrays.toString(location) + ", id=" + id
                + ", description=" + description + ", weight=" + weight
                + ", name=" + name + ", exit=" + Arrays.toString(exit)
                +"]");
          }
       return buffer.toString();
     }

}

此代码是一个简化的 Visitor Pattern哪里

'Visitor' -> your predicate

'this' -> 'this.id'

这是有效的,因为您的 toString() 正在调用嵌套 Location 对象的 toString(),这些对象也有用于过滤集的谓词。

如果您无法控制可以传播过滤器的结构,那么您可以采用这种方法:

 public String toString() {
           StringBuilder buffer = new StringBuilder();
           int i = 0;
           for(Location l = this; i < locations.length; l = locations[i++])
              if(filter.apply(l.id) { 
                 buffer.append("Location [location=" + Arrays.toString(location) + ", id=" + id
                    + ", description=" + description + ", weight=" + weight
                    + ", name=" + name + ", exit=" + Arrays.toString(exit)
                    +"]");
              }
           return buffer.toString();
         }

关于java - 如何按字段打印类对象的内容?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29379998/

相关文章:

java - 当其依赖仍在运行时执行 gradle 任务

java - 计算JPA中2个日期之间的天数

javascript - 在 javascript 中使用 PHP 生成 JSON : issues with backslashes

c# - 防止 Microsoft OData 客户端请求完整元数据

java - Jackson 枚举反序列化

java - spring-json 与 jackson 的 spring 框架 3.0.5

Java - FutureTask 不工作?

java - GridLayout 中的单元格内容居中

java - 来自 Api 调用的 Java 中的 Json 建模

java - jackson deearlization : two keys at root. 我如何打开一个并忽略另一个?