漂亮的输出总是插入" \ r \ n"

时间:2016-09-04 07:55:24

标签: java json jackson

我正在开发费用计算器。之前,我有一个工作的XML输出,没有打印换行符。我觉得有些事情搞砸了,也许编辑没有看到换行是一个换行符,不知道。

但是当我节省开支时,我的输出非常难看:

"[ {\r\n \"title\" : \"2\",\r\n \"category\" : \"[None]\",\r\n \"period\" : \"Year\",\r\n \"value\" : \"2\"\r\n}, {\r\n \"title\" : \"3\",\r\n \"category\" : \"[None]\",\r\n \"period\" : \"Year\",\r\n \"value\" : \"3\"\r\n} ]"

如果我看一下控制台(在那里打印),我得到了我想要的东西:

[ { "title" : "2", "category" : "[None]", "period" : "Year", "value" : "2" }, { "title" : "3", "category" : "[None]", "period" : "Year", "value" : "3" } ]

目前,我的代码非常简单:

ObservableList<Expense> expenseList  = FXCollections.observableArrayList();
//The list gets edited by a TableView.
final ObjectMapper mapper = new ObjectMapper();
final String s = mapper.writerWithDefaultPrettyPrinter().writeValueAsString(expenseList);
FileOutputStream fos = new FileOutputStream(path);
OutputStreamWriter outputFile = new OutputStreamWriter(fos, Charset.forName("UTF8"));

mapper.writeValue(outputFile, s);

这是实体类的外观:

public class Expense {
    private StringProperty title = new SimpleStringProperty();
    private StringProperty category = new SimpleStringProperty();
    private StringProperty period = new SimpleStringProperty();
    private StringProperty value = new SimpleStringProperty();

    public void setTitle(String title) {
        this.title.set(title);
    }

    public void setCategory(String category) {
        this.category.set(category);
    }

    public void setPeriod(String period) {
        this.period.set(period);
    }

    public void setValue(String value) {
        this.value.set(value);
    }

    public Expense() {} //Default constructor is needed for XML-handling

    public Expense(String title, String value, String period, String category) {
        this.title = new SimpleStringProperty(title);
        this.value = new SimpleStringProperty(value);
        this.period = new SimpleStringProperty(period);
        this.category = new SimpleStringProperty(category);
    }

    public String getTitle() {
        return this.title.get();
    }

    public String getCategory() {
        return this.category.get();
    }

    public String getPeriod() {
        return this.period.get();
    }

    public String getValue() {
        return this.value.get();
    }

    public StringProperty titleProperty() {
        return this.title;
    }

    public StringProperty categoryProperty() {
        return this.category;
    }

    public StringProperty periodProperty() {
        return this.period;
    }

    public StringProperty valueProperty() {
        return this.value;
    }
}

我试图获取列表中的每一个并将其转换为json字符串 - &gt;把它连成我打印出的一个大的单个字符串。但结果相同。

1 个答案:

答案 0 :(得分:1)

这一行做什么

mapper.writeValue(outputFile, s);

获取String并将其作为JSON消息中的值写入。如果该String已包含特殊字符,则必须将它们转义出来。

您最想要的是

 try (FileOutputStream fos = new FileOutputStream(path);
      OutputStreamWriter outputFile = new OutputStreamWriter(fos, StandardCharsets.UTF_8)) {
    outputFile .write(s); // just write the String as it is
}