
- 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
When can we use a JSONStringer in Java?
A JSONStringer provides a convenient way of producing JSON text and it can strictly follow to JSON syntax rules. Each instance of JSONStringer can produce one JSON text. A JSONStringer instance provides a value-method for appending values to the text and a key-method for adding keys before values in objects. There is an array () and endArray() methods that make and bound array values and object() and end object() methods that make and bound object values.
Example 1
import org.json.*; public class JSONStringerTest1 { public static void main(String[] args) throws JSONException { JSONStringer stringer = new JSONStringer(); String jsonStr = stringer .object() // Start JSON Object .key("Name") .value("Raja") .key("Age") //Add key-value pairs .value("25") .key("City") .value("Hyderabad") .endObject() // End JSON Object .toString(); System.out.println(jsonStr); } }
Output
{"Name":"Raja","Age":"25","City":"Hyderabad"}
Example 2
import org.json.*; public class JSONStringerTest2 { public static void main(String[] args) throws JSONException { JSONStringer stringer = new JSONStringer(); String jsonStr = stringer .array() //Start JSON Array .object() //Start JSON Object .key("Name").value("Adithya") .key("Age").value("25") //Add key-value pairs .key("Mobile").value("9959984000") .endObject() //End JSON Object .object() .key("Address").value("Madhapur") .key("City").value("Hyderabad") .endObject() .endArray() //End JSON Array .toString(); System.out.println(jsonStr); } }
Output
[{"Name":"Adithya","Age":"25","Mobile":"9959984000"},{"Address":"Madhapur","City":"Hyderabad"}]
- Related Articles
- When can we use Synchronized blocks in Java?
- When can we use the pack() method in Java?
- When can we use the getClass() method in Java?
- When can we use StackWalker.getCallerClass() method in Java 9?
- When can we use intern() method of String class in Java?
- In SAP we can use the Java Connector
- Can we use Comparator with list in Java?
- Can we use "this" keyword in a static method in java?
- Can we use Switch statement with Strings in java?
- How can we use the StringTokenizer class in Java?
- Can we use “When” as column name in CREATE TABLE statement?
- Why do we need a copy constructor and when should we use a copy constructor in Java?
- How can we use @Since annotation using Gson in Java?
- Can we use private methods in an interface in Java 9?
- How can we use a diamond operator with anonymous classes in Java 9?

Advertisements