java—是否可以使用jacksonjsonparser一次将单个事件信息存储在jsonobject/jsonnode中

t3irkdon  于 2021-07-09  发布在  Java
关注(0)|答案(0)|浏览(208)

我正在尝试使用 Jackson JsonParser . 我想将每个事件临时存储在一个对象中,例如 JsonObject 或者任何其他我以后想用于进一步处理的对象。
我以前在看报纸 JSON 事件一个接一个地存储到我自己的自定义上下文中:jacksonjsonparser上下文的旧文章,它运行良好。但是,与上下文不同,我希望将它们逐个存储到jsonobject或其他对象中。
以下是我的示例json文件:

{
   "@context":"https://context.org/context.jsonld",
   "isA":"SchoolManagement",
   "format":"application/ld+json",
   "schemaVersion":"2.0",
   "creationDate":"2021-04-21T10:10:09+00:00",
   "body":{
      "members":[
         {
            "isA":"student",
            "name":"ABCS",
            "class":10,
            "coaching":[
              "XSJSJ",
              "IIIRIRI"
            ],
            "dob":"1995-04-21T10:10:09+00:00"
         },
         {
            "isA":"teacher",
            "name":"ABCS",
            "department":"computer science",
            "school":{
              "name":"ABCD School"
            },
            "dob":"1995-04-21T10:10:09+00:00"
         },
         {
            "isA":"boardMember",
            "name":"ABCS",
            "board":"schoolboard",
            "dob":"1995-04-21T10:10:09+00:00"
         }
      ]
   }
}

一次我只想储存一个 member 例如 student 或者 teacher 在我的 JsonObject .
以下是迄今为止我所掌握的代码:将每个事件存储在一个对象中的最佳方法是什么,我以后可以使用它进行一些处理。然后再次清除该对象并将其用于下一个事件?

public class Main {

    private JSONObject eventInfo;
    private final String[] eventTypes = new String[] { "student", "teacher", "boardMember" };

    public static void main(String[] args) throws JsonParseException, JsonMappingException, IOException, JAXBException, URISyntaxException {
        // Get the JSON Factory and parser Object
        JsonFactory jsonFactory = new JsonFactory();
        JsonParser jsonParser = jsonFactory.createParser(new File(Main.class.getClassLoader().getResource("inputJson.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");
        }

        // Loop until the start of the EPCIS EventList array
        while (jsonParser.nextToken() != JsonToken.START_ARRAY) {
            System.out.println(jsonParser.getCurrentToken() + " --- " + jsonParser.getCurrentName());
        }

        // Goto the next token
        jsonParser.nextToken();

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

    // Method which will traverse through the eventList and read event one-by-one
    private static void eventTraverser(JsonParser jsonParser) throws IOException {

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

            //Is there a possibility to store the complete object directly in an JSON Object or I need to again go through every token to see if is array and handle it accordingly as mentioned in my previous POST.

        }
    }
}

暂无答案!

目前还没有任何答案,快来回答吧!

相关问题