Introduction to Orgjson

JavaJavaBeginner
Practice Now

Introduction

In this lab, you will learn how to work with JSON in Java using the org.json library. We will cover the classes and methods provided by the library, including how to create JSON objects and arrays, how to convert JSON data to other formats, and how to handle JSON exceptions.

Creating a JSONObject

A JSONObject is used to store key-value pairs. Here's an example of how to create a JSONObject:

import org.json.JSONObject;

public class Main {
  public static void main(String[] args) {
    JSONObject jo = new JSONObject();
    jo.put("name", "John");
    jo.put("age", 25);
    System.out.println(jo.toString());
  }
}

To run the code, execute the following command in your terminal.

javac Main.java && java Main

The above code will create a JSONObject with two key-value pairs: name and age, and then print it to the console.

Creating a JSONArray

A JSONArray is an ordered collection of values. Here's an example of how to create a JSONArray:

import org.json.JSONArray;

public class Main {
  public static void main(String[] args) {
    JSONArray ja = new JSONArray();
    ja.put(10);
    ja.put("A string");
    System.out.println(ja.toString());
  }
}

To run the code, execute the following command in your terminal.

javac Main.java && java Main

The above code will create a JSONArray with two values: 10 and "A string", and then print it to the console.

Converting JSON to String

To convert a JSONObject or JSONArray to a string, use the toString() method. Here's an example:

import org.json.JSONObject;

public class Main {
  public static void main(String[] args) {
    JSONObject jo = new JSONObject();
    jo.put("name", "John");
    jo.put("age", 25);

    String jsonString = jo.toString();
    System.out.println(jsonString);
  }
}

To run the code, execute the following command in your terminal.

javac Main.java && java Main

The above code will create a JSONObject with two key-value pairs, convert it to a string, and then print the string to the console.

Converting JSON to Java Objects

The org.json library provides convenient methods for converting JSON data to Java objects. For example, you can use the get methods of JSONObject to get the values of specific keys as Java objects. Here's an example:

import org.json.JSONObject;

public class Main {
  public static void main(String[] args) {
    JSONObject jo = new JSONObject();
    jo.put("name", "John");
    jo.put("age", 25);

    String name = jo.getString("name");
    int age = jo.getInt("age");

    System.out.println("Name: " + name);
    System.out.println("Age: " + age);
  }
}

To run the code, execute the following command in your terminal.

javac Main.java && java Main

The above code will create a JSONObject with two key-value pairs, get the values of the keys "name" and "age", and then print them to the console.

Creating a JSONObject from a String

If you have a valid JSON string, you can use it to create a JSONObject. Here's an example:

import org.json.JSONObject;

public class Main {
  public static void main(String[] args) {
    String jsonString = "{\"name\":\"John\",\"age\":25}";

    JSONObject jo = new JSONObject(jsonString);
    System.out.println(jo.toString());
  }
}

To run the code, execute the following command in your terminal.

javac Main.java && java Main

The above code will create a JSONObject from the JSON string, and then print it to the console.

Creating a JSONArray from a String

If you have a valid JSON string, you can use it to create a JSONArray. Here's an example:

import org.json.JSONArray;

public class Main {
  public static void main(String[] args) {
    String jsonString = "[10,\"A string\"]";

    JSONArray ja = new JSONArray(jsonString);
    System.out.println(ja.toString());
  }
}

To run the code, execute the following command in your terminal.

javac Main.java && java Main

The above code will create a JSONArray from the JSON string, and then print it to the console.

Handling JSON Exceptions

When working with org.json, you need to handle JSONExceptions. These exceptions are thrown when you try to perform an invalid operation on a JSON object or array. Here's an example:

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

public class Main {
  public static void main(String[] args) {
    try {
      JSONObject jo = new JSONObject();
      jo.put("name", "John");
      System.out.println(jo.getInt("name"));
    } catch (JSONException e) {
      System.out.println("JSONException: " + e.getMessage());
    }
  }
}

To run the code, execute the following command in your terminal.

javac Main.java && java Main

The above code will create a JSONObject and try to get an int value for the key "name", which is a String. This will result in a JSONException, which will be caught and printed to the console.

Summary

In this lab, you learned how to work with JSON in Java using the org.json library. You learned how to create JSON objects and arrays, how to convert JSON data to other formats, and how to handle JSON exceptions. You also learned how to import the org.json library and convert JSON data to Java objects.

Other Java Tutorials you may like