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

时间:2015-03-31 22:58:58

标签: java json jackson pojo

我有一个POJO类Location,用于使用Jackson将JSON文件映射到类。当前的实现可以通过调用Location toString()打印出类中的每个Location对象,但我想知道如何打印,例如,id =" 2&#34的位置;,这将是名称="沙漠"

目前,我使用这样的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)
                +"]";
    }



}

3 个答案:

答案 0 :(得分:1)

您可以尝试gson,它输入一个对象并输出JSON或在另一侧。

在使对象成为JSONObject之后,您可以遍历JSON以遍历对象。

答案 1 :(得分:1)

Stream.of(location).filters(l -> l.getId() == 2).foreach(System.out::println);

这有用吗?

答案 2 :(得分:1)

<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,其中

  

'访客' - &gt;你的谓词

     

'this' - &gt; '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();
         }