All TalkersCode Topics

Follow TalkersCode On Social Media

devloprr.com - A Social Media Network for developers Join Now ➔

Java Parse JSON String

Last Updated : Mar 11, 2024

Java Parse JSON String

In this article we will show you the solution of java parse JSON string, a lightweight, text-based, language-independent data transmission format, JSON (JavaScript Object Notation) is simple for both people and machines to read and write.

Objects and arrays are two structured kinds that JSON can represent. An object is a collection of zero or more name/value pairs that is not ordered.

An ordered series of zero or more values is referred to as an array. Strings, integers, booleans, null, and these two structured kinds are among the possible values.

The JSON representation of an object that defines a person is seen in the straightforward Wikipedia example that is provided below.

The object's first and last names are represented as string values, along with the person's age, address, and an array of phone number objects.

We will now discuss the idea of how to parse json string in java with an example.

Step by step guide on java parse JSON string :-

import java.io.FileNotFoundException;
import java.io.PrintWriter;
import java.util.LinkedHashMap;
import java.util.Map;
import org.json.simple.JSONArray;
import org.json.simple.JSONObject;
public class TalkersCode
{
    public static void main(String[] args) throws FileNotFoundException
    {
        JSONObject jo = new JSONObject();
        jo.put("firstname", "Amruta");
        jo.put("lastname", "Shahane");
        jo.put("age", 25);
        Map m = new LinkedHashMap(4);
        m.put("streetAddress", "21 2nd Street");
        m.put("city", "Pune");
        m.put("state", "P");
        m.put("postalCode", ******);
        jo.put("address", m);
        JSONArray ja = new JSONArray();
        m = new LinkedHashMap(2);
        m.put("type", "home");
        m.put("number", "9876543210");
        ja.add(m);
        m = new LinkedHashMap(2);
        m.put("type", "fax");
        m.put("number", "212 555-1234");
        ja.add(m);
        jo.put("phoneNumbers", ja);
        PrintWriter pw = new PrintWriter("JSONExample.json");
        pw.write(jo.toJSONString());
        pw.flush();
        pw.close();
    }
}
  1. We begin by importing the necessary libraries, which include the PrintWriter class, the JSONObject and JSONArray from the JSON.simple package.
  2. The main function is defined, along with a declaration that it throws a FileNotFoundException.
  3. To store the person's information, we construct a JSONObject called "jo."
  4. First name, last name, and age are added as key-value pairs using the put() method.
  5. To store the person's address information, we create a LinkedHashMap called "m."
  6. Street address, city, state, and postal code key-value pairs are added using the put() method.
  7. To add the "address" key and the LinkedHashMap "m" to the JSONObject "jo," we once more utilize the put() function.
  8. To hold the person's phone numbers, we create a JSONArray we will call "ja".
  9. In order to hold a single phone number, we establish a new LinkedHashMap we call "m".
  10. For the phone number type (home) and number, we add key-value pairs using the put() method.
  11. The JSONArray "ja" is added to the LinkedHashMap "m" using the add() method.
  12. In order to hold another phone number, we established a new LinkedHashMap we call "m".
  13. We add key-value pairs for the phone number type (fax) and number using the put() method.
  14. The JSONArray "ja" is added to the LinkedHashMap "m" using the add() method.
  15. The JSONArray "ja" and the "phoneNumbers" key are added to the JSONObject "jo" using the put() function once more.
  16. We make a PrintWriter object called "pw" and give it the destination filename "JSONExample.json."
  17. The JSONObject "jo" is written to the file as a string using the write() method.
  18. To make sure the data is written to the file, we flush and close the PrintWriter object.

Conclusion :-

As a result, we have successfully learned how to parse json string in java with an example.

It is clear from the explanation above that there are several ways to parse JSON in Java. Utilizing one strategy over the other has some benefits that we have observed.

The Jackson library is the greatest of all since it can parse enormous volumes of data, is simple and memory-efficient, and is frequently used in production applications.

I hope this article on java parse JSON string helps you and the steps and method mentioned above are easy to follow and implement.

Author Image About Ashish

Ashish is a dynamic and motivated individual with a passion of programming and an experienced programmer having 3+ years of experience in various languages like Java, Python, HTML, CSS, JavaScript, jQuery and various frameworks like Bootstrap

Follow Ashish On Linkedin 🡪