Skip to content
Related Articles

Related Articles

Improve Article
Save Article
Like Article

Java Program to Implement a JSON Parser

  • Last Updated : 30 Sep, 2021

JSON is commonly used in order to exchange data to and back fro from a  web server. The key point here is when we are getting the data from the webserver then it is returned always as a string so do we need t take care in our java program. 

Attention reader! Don’t stop learning now. Get hold of all the important Java Foundation and Collections concepts with the Fundamentals of Java and Java Collections Course at a student-friendly price and become industry ready. To complete your preparation from learning a language to DS Algo and many more,  please refer Complete Interview Preparation Course.

Illustration:

{"Geeks", "NIT", "Male", "30"}

This data is returned by JSON parser as a JavaScript object so do it becomes as follows:



const object = JSON.parse({"Geeks", "NIT", "Male", "30"}) ;

So do the same dealing is computed with arrays while JSON s parsing data. Let us write a java program invoking methods to make it simpler to grasp how JSON parser is implemented.

Example

Java




// Java Program to Implement JSON Parser
 
// Importing required classes
import java.util.ArrayList;
import java.util.Collections;
import java.util.HashMap;
 
// Defining constants for json parsers
enum CONSTANTS {
 
    CURLY_OPEN_BRACKETS('{'),
    CURLY_CLOSE_BRACKETS('}'),
    SQUARE_OPEN_BRACKETS('['),
    SQUARE_CLOSE_BRACKETS(']'),
    COLON(':'),
    COMMA(','),
    SPECIAL('|');
 
    private final char constant;
 
    // Constructor
    CONSTANTS(char constant) { this.constant = constant; }
 
    // Method
    // Overriding exiting toString() method
    @Override public String toString()
    {
        return String.valueOf(constant);
    }
}
 
// Class 1
// To parse json object
class JSONObject {
 
    private final static char specialChar;
    private final static char commaChar;
    private HashMap<String, String> objects;
 
    static
    {
        specialChar = String.valueOf(CONSTANTS.SPECIAL)
                          .toCharArray()[0];
        commaChar = String.valueOf(CONSTANTS.COMMA)
                        .toCharArray()[0];
    }
 
    // Constructor if this class
    public JSONObject(String arg) { getJSONObjects(arg); }
 
    // Method 1
    // Storing json objects as key value pair in hash map
    public void getJSONObjects(String arg)
    {
 
        objects = new HashMap<String, String>();
 
        if (arg.startsWith(String.valueOf(
                CONSTANTS.CURLY_OPEN_BRACKETS))
            && arg.endsWith(String.valueOf(
                CONSTANTS.CURLY_CLOSE_BRACKETS))) {
 
            StringBuilder builder = new StringBuilder(arg);
            builder.deleteCharAt(0);
            builder.deleteCharAt(builder.length() - 1);
            builder = replaceCOMMA(builder);
 
            for (String objects : builder.toString().split(
                     String.valueOf(CONSTANTS.COMMA))) {
 
                String[] objectValue = objects.split(
                    String.valueOf(CONSTANTS.COLON), 2);
 
                if (objectValue.length == 2)
                    this.objects.put(
                        objectValue[0]
                            .replace("'", "")
                            .replace("\"", ""),
                        objectValue[1]
                            .replace("'", "")
                            .replace("\"", ""));
            }
        }
    }
 
    // Method 2
    public StringBuilder replaceCOMMA(StringBuilder arg)
    {
 
        boolean isJsonArray = false;
 
        for (int i = 0; i < arg.length(); i++) {
            char a = arg.charAt(i);
 
            if (isJsonArray) {
 
                if (String.valueOf(a).compareTo(
                        String.valueOf(CONSTANTS.COMMA))
                    == 0) {
                    arg.setCharAt(i, specialChar);
                }
            }
 
            if (String.valueOf(a).compareTo(String.valueOf(
                    CONSTANTS.SQUARE_OPEN_BRACKETS))
                == 0)
                isJsonArray = true;
            if (String.valueOf(a).compareTo(String.valueOf(
                    CONSTANTS.SQUARE_CLOSE_BRACKETS))
                == 0)
                isJsonArray = false;
        }
 
        return arg;
    }
 
    // Method 3
    // Getting json object value by key from hash map
    public String getValue(String key)
    {
        if (objects != null) {
            return objects.get(key).replace(specialChar,
                                            commaChar);
        }
        return null;
    }
 
    // Method 4
    // Getting json array by key from hash map
    public JSONArray getJSONArray(String key)
    {
        if (objects != null)
            return new JSONArray(
                objects.get(key).replace('|', ','));
        return null;
    }
}
 
// Class 2
// To parse json array
class JSONArray {
 
    private final static char specialChar;
    private final static char commaChar;
 
    private ArrayList<String> objects;
 
    static
    {
        specialChar = String.valueOf(CONSTANTS.SPECIAL)
                          .toCharArray()[0];
        commaChar = String.valueOf(CONSTANTS.COMMA)
                        .toCharArray()[0];
    }
 
    // Constructor of this class
    public JSONArray(String arg) { getJSONObjects(arg); }
 
    // Method 1
    // Storing json objects in array list
    public void getJSONObjects(String arg)
    {
 
        objects = new ArrayList<String>();
 
        if (arg.startsWith(String.valueOf(
                CONSTANTS.SQUARE_OPEN_BRACKETS))
            && arg.endsWith(String.valueOf(
                CONSTANTS.SQUARE_CLOSE_BRACKETS))) {
 
            StringBuilder builder = new StringBuilder(arg);
 
            builder.deleteCharAt(0);
            builder.deleteCharAt(builder.length() - 1);
 
            builder = replaceCOMMA(builder);
 
            // Adding all elements
            // using addAll() method of Collections class
            Collections.addAll(
                objects,
                builder.toString().split(
                    String.valueOf(CONSTANTS.COMMA)));
        }
    }
 
    // Method 2
    public StringBuilder replaceCOMMA(StringBuilder arg)
    {
        boolean isArray = false;
 
        for (int i = 0; i < arg.length(); i++) {
            char a = arg.charAt(i);
            if (isArray) {
 
                if (String.valueOf(a).compareTo(
                        String.valueOf(CONSTANTS.COMMA))
                    == 0) {
                    arg.setCharAt(i, specialChar);
                }
            }
 
            if (String.valueOf(a).compareTo(String.valueOf(
                    CONSTANTS.CURLY_OPEN_BRACKETS))
                == 0)
                isArray = true;
 
            if (String.valueOf(a).compareTo(String.valueOf(
                    CONSTANTS.CURLY_CLOSE_BRACKETS))
                == 0)
                isArray = false;
        }
 
        return arg;
    }
 
    // Method  3
    // Getting json object by index from array list
    public String getObject(int index)
    {
        if (objects != null) {
            return objects.get(index).replace(specialChar,
                                              commaChar);
        }
 
        return null;
    }
 
    // Method 4
    // Getting json object from array list
    public JSONObject getJSONObject(int index)
    {
 
        if (objects != null) {
            return new JSONObject(
                objects.get(index).replace('|', ','));
        }
 
        return null;
    }
}
 
// Class 3
// To parse json string
public class Parser {
 
    // json string with user data
    // Custom data been passed as in arguments
    private final static String jsonString
        = "{'name':'user','id':1234,'marks':[{'english':85,'physics':80,'chemistry':75}]}";
 
    // Main driver method
    public static void main(String[] args)
    {
 
        // Parse json object for user data
        JSONObject user = new JSONObject(jsonString);
 
        // Get json array for user's marks
        JSONArray marks = user.getJSONArray("marks");
 
        // Get json object for subject's marks
        JSONObject subjects = marks.getJSONObject(0);
 
        // Print and display commands
        System.out.println(
            String.format("English marks - %s",
                          subjects.getValue("english")));
        System.out.println(
            String.format("Physics marks - %s",
                          subjects.getValue("physics")));
        System.out.println(
            String.format("Chemistry marks - %s",
                          subjects.getValue("chemistry")));
    }
}


Output

English marks - 85
Physics marks - 80
Chemistry marks - 75

 




My Personal Notes arrow_drop_up
Recommended Articles
Page :

Start Your Coding Journey Now!