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(); } }
- We begin by importing the necessary libraries, which include the PrintWriter class, the JSONObject and JSONArray from the JSON.simple package.
- The main function is defined, along with a declaration that it throws a FileNotFoundException.
- To store the person's information, we construct a JSONObject called "jo."
- First name, last name, and age are added as key-value pairs using the put() method.
- To store the person's address information, we create a LinkedHashMap called "m."
- Street address, city, state, and postal code key-value pairs are added using the put() method.
- To add the "address" key and the LinkedHashMap "m" to the JSONObject "jo," we once more utilize the put() function.
- To hold the person's phone numbers, we create a JSONArray we will call "ja".
- In order to hold a single phone number, we establish a new LinkedHashMap we call "m".
- For the phone number type (home) and number, we add key-value pairs using the put() method.
- The JSONArray "ja" is added to the LinkedHashMap "m" using the add() method.
- In order to hold another phone number, we established a new LinkedHashMap we call "m".
- We add key-value pairs for the phone number type (fax) and number using the put() method.
- The JSONArray "ja" is added to the LinkedHashMap "m" using the add() method.
- The JSONArray "ja" and the "phoneNumbers" key are added to the JSONObject "jo" using the put() function once more.
- We make a PrintWriter object called "pw" and give it the destination filename "JSONExample.json."
- The JSONObject "jo" is written to the file as a string using the write() method.
- 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.