【问题标题】:Java Lambda Expression message formatting, break and string concatenationJava Lambda 表达式消息格式化、中断和字符串连接
【发布时间】:2017-05-01 18:28:20
【问题描述】:

只是一个关于 Lambda 表达式的快速问题。我有以下文字:

{"hosts":[{"disks":[{"path":"/","space_used":0.608}],"hostname":"host1"},{"disks":[{"path ":"/","space_used":0.79},{"path":"/opt","space_used":0.999}],"hostname":"host2"},{"disks":[{"path" :"/","space_used":0.107}],"hostname":"host3"}]}

我想将上面的内容格式化为每一行阅读:

{"hostname": "host1", "disks":["Path '/' has used 60.8%"]}

{"hostname": "host2", "disks":["Path '/' has used 79%", "Path '/opt' has used 99.9%"]}

{"hostname": "host3", "disks":["Path '/' has used 10.7%"]}

我在 .stream().map() 和 .collect() 周围尝试了一些排列,但不知何故,我无法获得所需的输出。如果问题相当无趣,我们将不胜感激任何帮助并道歉。谢谢。

【问题讨论】:

  • 为什么不使用 JSON 库?

标签: java lambda java-8 java-stream


【解决方案1】:

您可以使用Jackson 2 Library 将您的 JSON 字符串转换为 Java 对象。

在您的具体情况下,您可以例如创建以下类:


Disk.java

@JsonInclude(JsonInclude.Include.NON_NULL)
@JsonPropertyOrder({"path", "space_used"})
public class Disk implements Serializable {

  private final static long serialVersionUID = -6127352847480270783L;

  @JsonProperty("path")
  private String path;

  @JsonProperty("space_used")
  private Double spaceUsed;

  @JsonIgnore
  private Map<String, Object> additionalProperties = new HashMap<String, Object>();

  @JsonProperty("path")
  public String getPath() {
    return path;
  }

  @JsonProperty("path")
  public void setPath(String path) {
    this.path = path;
  }

  @JsonProperty("space_used")
  public Double getSpaceUsed() {
    return spaceUsed;
  }

  @JsonProperty("space_used")
  public void setSpaceUsed(Double spaceUsed) {
    this.spaceUsed = spaceUsed;
  }

  @JsonAnyGetter
  public Map<String, Object> getAdditionalProperties() {
    return this.additionalProperties;
  }

  @JsonAnySetter
  public void setAdditionalProperty(String name, Object value) {
    this.additionalProperties.put(name, value);
  }
}

Host.java

@JsonInclude(JsonInclude.Include.NON_NULL)
@JsonPropertyOrder({"disks", "hostname"})
public class Host implements Serializable {

  private final static long serialVersionUID = -1972892789688333505L;

  @JsonProperty("disks")
  private List<Disk> disks = null;

  @JsonProperty("hostname")
  private String hostname;

  @JsonIgnore
  private Map<String, Object> additionalProperties = new HashMap<String, Object>();

  @JsonProperty("disks")
  public List<Disk> getDisks() {
    return disks;
  }

  @JsonProperty("disks")
  public void setDisks(List<Disk> disks) {
    this.disks = disks;
  }

  @JsonProperty("hostname")
  public String getHostname() {
    return hostname;
  }

  @JsonProperty("hostname")
  public void setHostname(String hostname) {
    this.hostname = hostname;
  }

  @JsonAnyGetter
  public Map<String, Object> getAdditionalProperties() {
    return this.additionalProperties;
  }

  @JsonAnySetter
  public void setAdditionalProperty(String name, Object value) {
    this.additionalProperties.put(name, value);
  }
}

HostContainer.java

@JsonInclude(JsonInclude.Include.NON_NULL)
@JsonPropertyOrder({"hosts"})
public class HostContainer implements Serializable {

  private final static long serialVersionUID = 7917934809738573749L;

  @JsonProperty("hosts")
  private List<Host> hosts = null;

  @JsonIgnore
  private Map<String, Object> additionalProperties = new HashMap<String, Object>();

  @JsonProperty("hosts")
  public List<Host> getHosts() {
    return hosts;
  }

  @JsonProperty("hosts")
  public void setHosts(List<Host> hosts) {
    this.hosts = hosts;
  }

  @JsonAnyGetter
  public Map<String, Object> getAdditionalProperties() {
    return this.additionalProperties;
  }

  @JsonAnySetter
  public void setAdditionalProperty(String name, Object value) {
    this.additionalProperties.put(name, value);
  }

}

用法:

  public void parseMessage() throws IOException {
    String msg = "{\"hosts\":[{\"disks\":[{\"path\":\"/\",\"space_used\":0.608}]," +
                 "\"hostname\":\"host1\"},{\"disks\":[{\"path\":\"/\",\"space_used\":0.79}," +
                 "{\"path\":\"/opt\",\"space_used\":0.999}],\"hostname\":\"host2\"}," +
                 "{\"disks\":[{\"path\":\"/\",\"space_used\":0.107}],\"hostname\":\"host3\"}]}";
    ObjectMapper mapper = new ObjectMapper();

    // This object will contain all the information you need, access it using e.g. getHosts();
    HostContainer hostContainer = mapper.readValue(msg, HostContainer.class);
  }

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2012-05-19
    • 1970-01-01
    • 2023-04-08
    • 1970-01-01
    • 2021-12-07
    • 2016-06-23
    • 2018-11-24
    • 1970-01-01
    相关资源
    最近更新 更多