Home > Java > javaTutorial > How to merge two JSON strings sequentially using JsonParserSequence in Java?

How to merge two JSON strings sequentially using JsonParserSequence in Java?

王林
Release: 2023-09-16 15:49:02
forward
574 people have browsed it

How to merge two JSON strings sequentially using JsonParserSequence in Java?

JsonParserSequence is a helper class that can be used to create a parser containing two sub-parsers, which are arranged in a specific order. We can create a sequence using the static method createFlattened() of the JsonParserSequence class.

Syntax

public static JsonParserSequence createFlattened(JsonParser first, JsonParser second)
Copy after login

Example

import java.io.*;
import com.fasterxml.jackson.core.*;
import com.fasterxml.jackson.core.util.*;
public class JsonParserSequenceTest {
   public static void main(String[] args) throws JsonParseException, IOException {
      String jsonString1 = "{\"id\":\"101\", \"name\":\"Ravi Chandra\", \"address\":\"Pune\"}";
      String jsonString2 = "{\"id\":\"102\", \"name\":\"Raja Ramesh\", \"address\":\"Hyderabad\", \"contacts\":[{\"mobile\":\"9959984805\", \"home\":\"7702144400\"}]}";
      JsonFactory jsonFactory = new JsonFactory();
      JsonParser jsonParser1 = jsonFactory.createParser(jsonString1);
      JsonParser jsonParser2 = jsonFactory.createParser(jsonString2);
      JsonParserSequence jsonParserSequence = JsonParserSequence.createFlattened(jsonParser1, jsonParser2);
      JsonToken jsonToken = jsonParserSequence.nextToken();
         while(jsonToken != null) {
            switch(jsonToken) {
               case FIELD_NAME: System.out.println("Key field: " + jsonParserSequence.getText());
               break;
               case VALUE_FALSE:
               case VALUE_NULL:
               case VALUE_NUMBER_FLOAT:
               case VALUE_NUMBER_INT:
               case VALUE_STRING:
               case VALUE_TRUE: System.out.println("Key value: " + jsonParserSequence.getText());
               break;
            }
            jsonToken = jsonParserSequence.nextToken();
         }
      jsonParserSequence.close();
   }
}
Copy after login

Output

Key field: id
Key value: 101
Key field: name
Key value: Ravi Chandra
Key field: address
Key value: Pune
Key field: id
Key value: 102
Key field: name
Key value: Raja Ramesh
Key field: address
Key value: Hyderabad
Key field: contacts
Key field: mobile
Key value: 9959984805
Key field: home
Key value: 7702144400
Copy after login

The above is the detailed content of How to merge two JSON strings sequentially using JsonParserSequence in Java?. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:tutorialspoint.com
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template