使用Java中的Jackson解析JSON文件,并将信息一一写入单个对象 [英] Parsing JSON file using Jackson in Java and writing information one by one to an single Object

查看:153
本文介绍了使用Java中的Jackson解析JSON文件,并将信息一一写入单个对象的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试将大JSON/JSON-LD文件转换为XML.JSON文件将包含事件列表(并非所有事件都相同,并且每个事件可能具有不同的信息/数据).我想一个个地读取事件,并将信息存储在一个对象中(而不是为每个事件使用不同的对象).我想以父母和孩子的形式存储它.读取一个事件后,我想将其转换为JSON,然后转到下一个事件.

I am trying to convert the large JSON/JSON-LD file to XML. The JSON file will contain a list of events (not all events are the same and each may have different information/data). I would like to read the events one by one and store the information in a single Object (rather than having different objects for each event). I would like to store it in the form of Parent and Children. As soon as I read one event I would like to convert it to JSON then go to the next one.

我正在调查JACKSON,看起来很适合.但是,我正在调查此特定类

I am looking into JACKSON and seems like it fits this. However, I am looking into this particular class TokenBuffer which is actually kind of doing the same. It's reading the JSON file line by line and then trying to see if the incoming element is Array or Object based on that it's making various decisions.

我想确认是否可以直接使用该类传递JsonFile并获取父级和子级关系,该关系将存储在 JsonWriteContext 中,以后我可以将其转换为XML.

I wanted to confirm if I can use this Class directly to pass my JsonFile and get the Parent and child relationship which will be stored in JsonWriteContext later I can convert it to XML.

基本上,我的示例文件将在JSON中包含许多事件,我想一次读取它们,然后在单个对象中获取元素信息及其父子关系,例如 HashMap .

Basically, my sample file will have many events in the JSON which I would like to read one by one and then get the element info and its parent-children relationship in a Single Object such as maybe HashMap.

推荐答案

在尝试了一些操作之后,下面是我的代码,其中包含Json的Parent和Child元素:

After trying some of the things following is my code which will contain the Parent and Child elements from Json:

@Getter
@Setter
@NoArgsConstructor
public class JsonContext {
    private JsonContext parent;
    private String key;
    private String value;
    private int childCount;
    private Map<String, List<JsonContext>> children = new HashMap<String, List<JsonContext>>();

    // Constructor for Children Object
    JsonContext(String key, String value) {
        this.key = key;
        this.value = value;
    }

    // Constructor for Parent Object
    JsonContext(JsonContext parent, String key, String value) {
        this(key, value);
        this.parent = parent;
    }

    // Add child based on incoming element
    public JsonContext addChild(JsonContext context) {
        List<JsonContext> childValues = children.get(context.getKey());
        if (childValues == null) {
            childValues = new ArrayList<JsonContext>();
        }
        childValues.add(context);
        children.put(context.key, childValues);
        childCount++;
        return context;
    }

    // Get the parent and their subsequent children (test purpose only)
    @Override
    public String toString() {
        String s = key + children.entrySet().stream().map(e -> e.getKey() + " -- " + String.join(", ", e.getValue().stream().map(v -> v.toString())
                            .collect(Collectors.toList()))).collect(Collectors.toList());

        if (value.length() > 0) {
            final String chars = value.toString().trim();
            if (chars.length() > 0) {
                s = s + " = " + chars;
            }
        }
        return s;
    }

}

以上是将存储信息的上下文文件.下面是JSON解析器文件.

The above is the context file that will store the information. Below is the JSON parser file.

import java.io.File;
import java.io.IOException;
import java.net.URISyntaxException;
import java.util.Arrays;

import com.converter.xml2jsonld.test.JSONParser;
import com.fasterxml.jackson.core.JsonFactory;
import com.fasterxml.jackson.core.JsonParseException;
import com.fasterxml.jackson.core.JsonParser;
import com.fasterxml.jackson.core.JsonToken;

public class JsonEventsParser {

    private JsonContext context = null;
    private final String[] eventTypes = new String[] { "event1", "event2", "event3", "event4",
                        "event5" };

    public void jsonFileIterator() throws URISyntaxException, JsonParseException, IOException {
        // Get the JSON Factory and parser Object
        JsonFactory jsonFactory = new JsonFactory();
        JsonParser jsonParser = jsonFactory.createParser(new File(JSONParser.class.getClassLoader().getResource("inputJsonFile.json").toURI()));
        JsonToken current = jsonParser.nextToken();

        // Check the first element is Object
        if (current != JsonToken.START_OBJECT) {
            throw new IllegalStateException("Expected content to be an array");
        }

        // Call the method to loop until the end of the events file
        FileNavigator(jsonParser);
    }

    private void FileNavigator(JsonParser jsonParser) throws IOException {

        JsonToken current = jsonParser.getCurrentToken();

        // Loop until the end of the EPCIS events file
        while (jsonParser.nextToken() != JsonToken.END_OBJECT) {

            final JsonToken token = jsonParser.nextToken();
            final String name = jsonParser.getCurrentName();

            // Handling the fields with direct key value pairs
            if ((token == JsonToken.FIELD_NAME || token == JsonToken.VALUE_STRING)) {
                writeFieldName(jsonParser, token);
            }

            // Handling the Object
            if (token == JsonToken.START_OBJECT) {
                writeObjectFields(jsonParser, token);
            }

            // Handling the Array
            if (token == JsonToken.START_ARRAY) {
                writeArrayFields(jsonParser, token);
            }

            if (context != null) {
                if (context.getParent() != null) {
                    context = context.getParent();
                }
            }
        }
        System.out.println(context.getChildren().toString());
    }

    // Method to obtain the STRING field and write into Context
    private void writeFieldName(JsonParser jsonParser, JsonToken token) throws IOException {
        final String key = jsonParser.getCurrentName();
        final String value = jsonParser.getValueAsString();

        // Check for the eventType
        if (context == null && Arrays.asList(eventTypes).contains(value)) {
            context = new JsonContext(key, value);
        } else if (context != null) {
            context = context.addChild(new JsonContext(context, key, value));
        }

    }

    // Method to obtain the OBJECT and write its children into Context
    private void writeObjectFields(JsonParser jsonParser, JsonToken token) throws IOException {

        final String objectParent = jsonParser.getCurrentName() == null ? context.getParent().getKey() : jsonParser.getCurrentName();
        // Add the name of the OBJECT
        if (context == null) {
            context = new JsonContext(jsonParser.getCurrentName(), "Object");
        } else if (context != null) {
            context = context.addChild(new JsonContext(context, objectParent, "Object"));
        }

        token = jsonParser.nextToken();

        // Loop through all elements within OBJECT and add them to its parent
        while (token != JsonToken.END_OBJECT) {
            final String key = jsonParser.getCurrentName();
            token = jsonParser.nextToken();
            // Check for incoming tokens within array and process accordingly
            switch (token) {
            case START_ARRAY:
                writeArrayFields(jsonParser, token);
                break;
            case START_OBJECT:
                writeObjectFields(jsonParser, token);
                break;
            default:
                final String value = jsonParser.getText();
                context = context.addChild(new JsonContext(context, key, value));
                break;
            // throw new RuntimeException("Object : Elements does not match the type
            // (Method: writeObjectFields)");
            }

            context = context.getParent();
            token = jsonParser.nextToken();
        }
    }

    // Method to Obtain the ARRAY and write its children into Context
    private void writeArrayFields(JsonParser jsonParser, JsonToken token) throws IOException {

        final String arrayField = jsonParser.getCurrentName();
        // Add the name of the ARRAY
        if (context == null) {
            context = new JsonContext(arrayField, "Array");
        } else if (context != null) {
            context = context.addChild(new JsonContext(context, arrayField, "Array"));
        }

        token = jsonParser.nextToken();

        // Loop through all ARRAY elements
        while (token != JsonToken.END_ARRAY) {

            switch (token) {
            case START_OBJECT:
                writeObjectFields(jsonParser, token);
                break;
            case VALUE_STRING:
                context = context.addChild(new JsonContext(context, arrayField, jsonParser.getText()));
                break;
            case START_ARRAY:
                writeArrayFields(jsonParser, token);
                break;
            default:
                throw new RuntimeException("Array : Elements does not match the type (Method: writeArrayFields)");
            }

            context = context.getParent();
            token = jsonParser.nextToken();
        }
    }
}

这篇关于使用Java中的Jackson解析JSON文件,并将信息一一写入单个对象的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆