
- Java Tutorial
- Java - Home
- Java - Overview
- Java - Environment Setup
- Java - Basic Syntax
- Java - Object & Classes
- Java - Constructors
- Java - Basic Datatypes
- Java - Variable Types
- Java - Modifier Types
- Java - Basic Operators
- Java - Loop Control
- Java - Decision Making
- Java - Numbers
- Java - Characters
- Java - Strings
- Java - Arrays
- Java - Date & Time
- Java - Regular Expressions
- Java - Methods
- Java - Files and I/O
- Java - Exceptions
- Java - Inner classes
- Java Object Oriented
- Java - Inheritance
- Java - Overriding
- Java - Polymorphism
- Java - Abstraction
- Java - Encapsulation
- Java - Interfaces
- Java - Packages
- Java Advanced
- Java - Data Structures
- Java - Collections
- Java - Generics
- Java - Serialization
- Java - Networking
- Java - Sending Email
- Java - Multithreading
- Java - Applet Basics
- Java - Documentation
- Java Useful Resources
- Java - Questions and Answers
- Java - Quick Guide
- Java - Useful Resources
- Java - Discussion
- Java - Examples
How to merge two JSON strings in an order using JsonParserSequence in Java?
The JsonParserSequence is a helper class that can be used to create a parser containing two sub-parsers placed in a particular sequence. We can create a sequence using the static method createFlattened() of the JsonParserSequence class.
Syntax
public static JsonParserSequence createFlattened(JsonParser first, JsonParser second)
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(); } }
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
- Related Articles
- How can we merge two JSON objects in Java?
- How can we merge two JSON arrays in Java?
- How to merge two strings alternatively in JavaScript
- How to concatenate two strings using Java?
- Largest Merge of Two Strings in C++
- Largest Merge of Two Strings in Python
- Program to merge two strings in alternating fashion in Python
- How to convert an array to JSON Array using JSON-lib API in Java?\n
- Program to find largest merge of two strings in Python
- Java code to print common characters of two Strings in alphabetical order
- Program to merge strings alternately using Python
- Merge two sets in Java
- How to convert a JSON object to an enum using Jackson in Java?
- How to add elements to JSON Object using JSON-lib API in Java?
- How to add a JSON string to an existing JSON file in Java?

Advertisements