Java 是否可以使用Jackson JsonParser在JSONObject/JsonNode中一次存储单个事件信息

Java 是否可以使用Jackson JsonParser在JSONObject/JsonNode中一次存储单个事件信息,java,json,jackson,jackson2,jsonparser,Java,Json,Jackson,Jackson2,Jsonparser,我正在尝试使用jacksonjsonparser从一个大的JSON文件中逐个读取事件。我想将每个事件临时存储在一个对象中,比如JsonObject,或者任何其他我以后想用于进一步处理的对象 我之前一个接一个地阅读了JSON事件,并将它们存储到我自己的自定义上下文中:这很好。但是,我希望将它们逐个存储到jsonObject或其他对象中,而不是存储到上下文中 以下是我的示例JSON文件: { "@context":"https://context.org/cont

我正在尝试使用
jacksonjsonparser
从一个大的JSON文件中逐个读取事件。我想将每个事件临时存储在一个对象中,比如
JsonObject
,或者任何其他我以后想用于进一步处理的对象

我之前一个接一个地阅读了
JSON
事件,并将它们存储到我自己的自定义上下文中:这很好。但是,我希望将它们逐个存储到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"
         }
      ]
   }
}
每次我只想在我的
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.
            
        }
    }
}

在尝试了一些东西之后,我能够让它工作。我发布整个代码是因为它对将来的人很有用,因为我知道找到合适的工作代码示例是多么令人沮丧:

public class Main
{
  public void xmlConverter (InputStream jsonStream) throws IOException,JAXBException, XMLStreamException
  {
    // jsonStream is the input JSOn which is normally passed by reading the JSON file
    
    // Get the JSON Factory and parser Object
    final JsonFactory jsonFactory = new JsonFactory ();
    final JsonParser jsonParser = jsonFactory.createParser (jsonStream);
    final ObjectMapper objectMapper = new ObjectMapper ();

    //To read the duplicate keys if there are any key duplicate json
    final SimpleModule module = new SimpleModule ();
      module.addDeserializer (JsonNode.class, new JsonNodeDupeFieldHandlingDeserializer ());
      objectMapper.registerModule (module);
      jsonParser.setCodec (objectMapper);

    // Check the first element is Object if not then invalid JSON throw error
    if (jsonParser.nextToken () != JsonToken.START_OBJECT)
      {
    throw new IllegalStateException ("Expected content to be an array");
      }

    while (!jsonParser.getText ().equals ("members"))
      {
    //Skipping the elements till members key
    // if you want you can do some process here
    // I am skipping for now
      }

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

    while (jsonParser.nextToken () != JsonToken.END_ARRAY)
      {
    final JsonNode jsonNode = jsonParser.readValueAsTree ();

    //Check if the JsonNode is valid if not then exit the process
    if (jsonNode == null || jsonNode.isNull ())
      {
        System.out.println ("End Of File");
        break;
      }

    // Get the eventType
    final String eventType = jsonNode.get ("isA").asText ();

    // Based on eventType call different type of class
    switch (eventType)
      {
      case "student":
        final Student studentInfo =
          objectMapper.treeToValue (jsonNode, Student.class);
        //I was calling the JAXB Method as I was doing the JSON to XML Conversion
        xmlCreator (studentInfo, Student.class);
        break;
      case "teacher":
        final Teacher teacherInfo =
          objectMapper.treeToValue (jsonNode, Teacher.class);
        xmlCreator (teacherInfo, Teacher.class);
        break;
      }
      }

  }

  //Method to create the XML using the JAXB
  private void xmlCreator (Object eventInfo,
               Class eventType) throws JAXBException
  {
    private final StringWriter sw = new StringWriter ();

    // Create JAXB Context object
    JAXBContext context = JAXBContext.newInstance (eventType);

    // Create Marshaller object from JAXBContext
    Marshaller marshaller = context.createMarshaller ();

    // Print formatted XML
      marshaller.setProperty (Marshaller.JAXB_FORMATTED_OUTPUT, Boolean.TRUE);

    // Do not add the <xml> version tag
      marshaller.setProperty (Marshaller.JAXB_FRAGMENT, Boolean.TRUE);

    // XmlSupportExtension is an interface that every class such as Student Teacher implements
    // xmlSupport is a method in XmlSupportExtension which has been implemented in all classes

    // Create the XML based on type of incoming event type and store in SW
      marshaller.marshal (((XmlSupportExtension) eventInfo).xmlSupport (),
              sw);

    // Add each event within the List
      eventsList.add (sw.toString ());

    // Clear the StringWritter for next event
      sw.getBuffer ().setLength (0);

  }
}

你在《杰克逊2》中试过树模型吗?

@JsonDeserialize(using = JsonNodeDupeFieldHandlingDeserializer.class)
public class JsonNodeDupeFieldHandlingDeserializer extends JsonNodeDeserializer {

  @Override
  protected void _handleDuplicateField(JsonParser p, DeserializationContext ctxt, JsonNodeFactory nodeFactory, String fieldName,
      ObjectNode objectNode, JsonNode oldValue, JsonNode newValue) {

    ArrayNode asArrayValue = null;

    if (oldValue.isArray()) {
      asArrayValue = (ArrayNode) oldValue;
    } else {
      asArrayValue = nodeFactory.arrayNode();
      asArrayValue.add(oldValue);
    }
    asArrayValue.add(newValue);
    objectNode.set(fieldName, asArrayValue);
  }

}