Importance of a JSONTokener in Java?\\n



If you have ever worked with JSON in Java, you might have used classes like JSONObject, JSONArray, JsonParser, etc. These classes are part of the org.json library. We use these classes to build JSON or parse JSON. But behind the scenes, these classes use a class called JSONTokener.

In this article, we will learn about the JSONTokener class and its importance in Java.

What is JSONTokener?

The JSONTokener class is like a tool that reads a JSON string step by step, breaking it into smaller parts called tokens. It is like a helper that makes it easier to understand and work with JSON strings.

It is part of the org.json library and is generally used to turn JSON strings into JSON objects. It works behind the scenes of other methods, and also makes the code less complex. Let's look at an example of how JSONTokener works. Normally, we write the JSONObject class like below:

JSONObject jsonObject = new JSONObject("{"name":"Ansh", "age":23, "city":"Delhi"}");

But what Java does behind is:

JSONObject jsonObject = new JSONObject(
   new JSONTokener("{"name":"Ansh", "age":23, "city":"Delhi"}")
);

Constructor of JSONTokener

The JSONTokener class has only one constructor, which takes a string as an argument. The string is the JSON string that we want to parse. Following is the constructor of the JSONTokener class:

JSONTokener(String s)

Where s is the JSON string that we want to parse.

Methods of JSONTokener

The JSONTokener class has the following methods:

  • next(): Reads the next character from the JSON string and gives it back as a single character.
  • next(int n): Reads the next n characters from the JSON string and gives them back as a string.
  • nextToken(): Reads the next meaningful part (token) from the JSON string and gives it back as a string.
  • skipTo(): Skips characters until it finds the one you are looking for.
  • back(): Moves back by one character in the JSON string.
  • close(): Closes the JSONTokener and frees up any resources it was using.
  • more(): Checks if there are more characters left to read in the JSON string.
  • read(): Reads the next character from the JSON string and gives it back as a number (int).

Let's take an example of how to use the JSONTokener class.

Example

Following is the code to demonstrate the use of the JSONTokener class:

import org.json.JSONTokener;
import org.json.JSONObject;
import org.json.JSONException;

public class JSONTokenerExample {
   public static void main(String[] args) {
      // Create a JSON string
      String jsonString = "{"name":"Ansh", "age":23, "city":"Delhi"}";

      // Create a JSONTokener object
      JSONTokener jsonTokener = new JSONTokener(jsonString);

      // Create a JSONObject object using JSONTokener
      JSONObject jsonObject = new JSONObject(jsonTokener);

      // Print the JSONObject
      System.out.println("JSONObject: " + jsonObject.toString(4));
   }
}

Following is the output of the above code:

JSONObject: {
   "name": "Ansh",
   "age": 23,
   "city": "Delhi"
}
Aishwarya Naglot
Aishwarya Naglot

Writing clean code… when the bugs aren’t looking.

Updated on: 2025-05-12T11:03:10+05:30

2K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements