Home  >  Article  >  Java  >  What is the importance of JSONTokener in Java?

What is the importance of JSONTokener in Java?

PHPz
PHPzforward
2023-09-07 08:49:061390browse

What is the importance of JSONTokener in Java?

#The JSONTokener class allows applications to break strings into tokens. It can be used by the JSONObject and JSONArray constructors to parse JSON source strings. Several important methods of the JSONTokener class include back() - Move the cursor one position backward, more() - Return true if the token has elements, otherwise return false, next() - Returns the next character at the current position, nextTo(character) - Returns the string before matching the given character. The Chinese translation of

Grammar

<strong>public class JSONTokener extends java.lang.Object</strong>

Example

is:

Example

import java.io.*;
import org.json.*;
public class JSONTokenerTest {
   public static void main(String args[]) throws JSONException, Exception {
      String jsonStr = "{" + " \"Technology\": \"Java\", "+ " \"location\": [ Madhapur, Hyderabad ] " + "}";
      <strong>JSONTokener </strong>jsonToken = new <strong>JSONTokener(jsonStr)</strong>;
      jsonToken.next();
      jsonToken.next();
      jsonToken.next();
      jsonToken.next();
      char c = jsonToken.<strong>next()</strong>;
      jsonToken.<strong>back()</strong>;
      boolean b = jsonToken.<strong>more()</strong>;
      String s = jsonToken.<strong>nextTo</strong>(&#39;i&#39;);
      System.out.println("next character: " + c);
      System.out.println("has more tokens: " + b);
      System.out.println("next to i: " + s);
   }
}

Output

<strong>next character: T
has more tokens: true
next to i: Technology": "Java", "locat</strong>

The above is the detailed content of What is the importance of JSONTokener in Java?. For more information, please follow other related articles on the PHP Chinese website!

Statement:
This article is reproduced at:tutorialspoint.com. If there is any infringement, please contact admin@php.cn delete