Jackson JSONGenerator 添加了换行符、反冲等,并且不按格式顺序显示给定的内容

Jackson JSONGenerator adds newline,backlash etc and does not display the given content in formatted order

我正在使用 JacksonXMLList 转换为 JSON 在转换来自 XML List<> 的每个事件后我存储转换后的JSON 变成 List<String>.

完成所有转换后,我必须创建一个带有一些外部元素的新 JSON,然后最后从 previously-stored List<String> 添加转换后的 JSON。一切正常,但是当我从 List<String> 添加 JSON 时,它还会添加 \n\,并且我的所有格式都消失了。我不确定为什么会这样。

我尝试搜索和使用提到的各种方法,但似乎没有任何效果,所以想在这里发帖。如果发现重复的,真的很抱歉。

代码如下:(请注意:这是我提供的示例直接代码,以便尝试的任何人都可以直接尝试找出问题。但是,已经给出了另一个具有完整工作流程的示例代码下面。只是为了说明我在我的应用程序中的实际表现。但是,两者都添加了 \ 并删除了格式。)

我只想知道如何避免将这些 \ 和其他 non-relevant 字符添加到我的 Final JSON 并为其添加格式。

public class Main {
    public static void main(String[] args) throws IOException {
        List<String> stringEvents = new ArrayList<>();
        stringEvents.add("{\n" +
                "  isA : \"Customer\",\n" +
                "  name : \"Rise Against\",\n" +
                "  age : \"2000\",\n" +
                "  google:sub : \"MyValue\",\n" +
                "  google:sub : \"MyValue\"\n" +
                "}");

        JsonFactory factory = new JsonFactory();
        StringWriter jsonObjectWriter = new StringWriter();
        JsonGenerator generator = factory.createGenerator(jsonObjectWriter);
        generator.writeStartObject();
        generator.writeStringField("schema", "1.0");
        generator.writeFieldName("eventList");
        generator.writeStartArray();
        stringEvents.forEach(event->{
            try {
                generator.writeObject(event);
            } catch (IOException e) {
                e.printStackTrace();
            }
        });
        generator.writeEndArray();
        generator.writeEndObject();
        generator.close();
        System.out.println(jsonObjectWriter.toString());
    }
}

以下是我得到的输出:

{"isA":"Customer","name":"Rise Against","age":"2000"}
{"schema":"1.0","eventList":["{\"isA\":\"Customer\",\"name\":\"Rise Against\",\"age\":\"2000\"}","{\"isA\":\"Customer\",\"name\":\"Rise Against\",\"age\":\"2000\"}"]}

以下是我的完整工作流程和代码:

  1. 我正在读取 XML 文件并执行 XML 到 Customer.classunmarshalling
  2. 我正在执行 JSON Conversion 并将转换后的 JSON 存储到 List<String>
  3. 完成所有转换后,我正在创建包含所有 header 信息的 Final JSON

以下是我的Customer.class

@JsonTypeInfo(use = JsonTypeInfo.Id.NAME, include = JsonTypeInfo.As.PROPERTY, visible = true, property = "isA")
@JsonInclude(Include.NON_NULL)
@JsonIgnoreProperties(ignoreUnknown = true)
@XmlRootElement(name = "extension")
@XmlType(name = "extension", propOrder = {"name", "age"})
@XmlAccessorType(XmlAccessType.FIELD)
@Getter
@Setter
@AllArgsConstructor
@ToString
@NoArgsConstructor
public class Customer {
    @XmlTransient
    private String isA;

    @XmlPath("customer/name/text()")
    private String name;

    @XmlPath("customer/age/text()")
    private String age;
}

以下是我的UnmarshalingJson Creation class

public class Unmarshalling {

    public static void main(String[] args) throws JAXBException, XMLStreamException, FactoryConfigurationError, IOException {
        final InputStream inputStream = Unmarshalling.class.getClassLoader().getResourceAsStream("customer.xml");
        final XMLStreamReader xmlStreamReader = XMLInputFactory.newInstance().createXMLStreamReader(inputStream);
        final Unmarshaller unmarshaller = JAXBContext.newInstance(Customer.class).createUnmarshaller();
        final Customer customer = unmarshaller.unmarshal(xmlStreamReader, Customer.class).getValue();
        final String jsonEvent = new ObjectMapper().writeValueAsString(customer);
        System.out.println(jsonEvent);
        List<String> stringEvents = new ArrayList<>();
        stringEvents.add(jsonEvent);
        stringEvents.add(jsonEvent);

        JsonFactory factory = new JsonFactory();
        StringWriter jsonObjectWriter = new StringWriter();
        JsonGenerator generator = factory.createGenerator(jsonObjectWriter);
        generator.writeStartObject();
        generator.writeStringField("schema", "1.0");
        generator.writeFieldName("eventList");
        generator.writeStartArray();
        stringEvents.forEach(event->{
            try {
                generator.writeObject(event);
            } catch (IOException e) {
                e.printStackTrace();
            }
        });
        generator.writeEndArray();
        generator.writeEndObject();
        generator.close();
        System.out.println(jsonObjectWriter.toString());

    }
}

以下是我的 xml 文件:

<extension xmlns:google="https://google.com">
    <customer>
        <name>Rise Against</name>
        <age>2000</age>
    </customer>
</extension>

在尝试了更多的东西后,我尝试了 generator.writeRaw(event); 并且成功了。

import com.fasterxml.jackson.core.JsonFactory;
import com.fasterxml.jackson.core.JsonGenerator;

import java.io.IOException;
import java.io.StringWriter;
import java.util.ArrayList;
import java.util.List;

public class Main {
    public static void main(String[] args) throws IOException {
        List<String> stringEvents = new ArrayList<>();
        stringEvents.add("{\n" +
                "  isA : \"Customer\",\n" +
                "  name : \"Rise Against\",\n" +
                "  age : \"2000\",\n" +
                "  google:sub : \"MyValue\",\n" +
                "  google:sub : \"MyValue\"\n" +
                "}");

        JsonFactory factory = new JsonFactory();
        StringWriter jsonObjectWriter = new StringWriter();
        JsonGenerator generator = factory.createGenerator(jsonObjectWriter);
        generator.writeStartObject();
        generator.writeStringField("schema", "1.0");
        generator.writeFieldName("eventList");
        generator.writeStartArray();
        stringEvents.forEach(event->{
            try {
                generator.writeRaw(event);
            } catch (IOException e) {
                e.printStackTrace();
            }
        });
        generator.writeEndArray();
        generator.writeEndObject();
        generator.close();
        System.out.println(jsonObjectWriter.toString());
    }
}