Home > Java > javaTutorial > How can we implement JSON array using streaming API in Java?

How can we implement JSON array using streaming API in Java?

WBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWB
Release: 2023-09-19 18:01:06
forward
741 people have browsed it

How can we implement JSON array using streaming API in Java?

The JsonGenerator interface can be used to stream JSON data to an output source. We can create or implement a JSON array using the JsonGenerator's writeStartArray() method, which writes a JSON name/start array character pair in the current object context. writeStartObject() The method writes the JSON starting object character, which is only valid in array context. The writeEnd() method writes the end of the current context.

Syntax

<strong>JsonGenerator writeStartArray(String name)</strong>
Copy after login

Example

import java.io.*;
import javax.json.*;
import javax.json.stream.*;
public class JsonGeneratorTest {
   public static void main(String[] args) throws Exception {
      StringWriter writer = new StringWriter();
      <strong>JsonGenerator </strong>jsonGen = <strong>Json.createGenerator</strong>(writer);
      jsonGen.<strong>writeStartObject()</strong>
             .<strong>write</strong>("name", "Adithya")
             .<strong>write</strong>("designation", "Python Developer")
             .<strong>write</strong>("company", "TutorialsPoint")
             .<strong>writeStartArray</strong>("personal details")
             .<strong>writeStartObject()</strong>
             .<strong>write</strong>("email", "adithya@gmail.com")
             .<strong>writeEnd()</strong>
             .<strong>writeStartObject()</strong>
             .<strong>write</strong>("contact", "9959927000")
             .<strong>writeEnd()  // end of object</strong>
             .<strong>writeEnd()  // end of an array</strong>
             .<strong>writeEnd()</strong>; // <strong>end of main object</strong>
      jsonGen.close();
      System.out.println(writer.toString());
   }
}
Copy after login

Output

<strong>{"name":"Adithya","designation":"Python Developer","company":"TutorialsPoint","personal details":[{"email":"adithya@gmail.com"},{"contact":"9959927000"}]}</strong>
Copy after login

The above is the detailed content of How can we implement JSON array using streaming API in Java?. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
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
Latest Issues
Install JAVA
From 1970-01-01 08:00:00
0
0
0
Unable to install java
From 1970-01-01 08:00:00
0
0
0
Can java be used as the backend of the web?
From 1970-01-01 08:00:00
0
0
0
Is this in Java language?
From 1970-01-01 08:00:00
0
0
0
Help: JAVA encrypted data PHP decryption
From 1970-01-01 08:00:00
0
0
0
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template