Factory Pattern is a design pattern to create objects based on the type with the help of Factory class. Factory Class create and return the object based on the type of request, and Caller class not aware of which class returned, however the created class do the operation based on the requested type. In this … Continue reading “Factory Design Pattern”
Author: admin
Singleton Design Pattern
A Singleton design Pattern is a very simple design pattern where only one instance(object) is created for a Class. And the Object can be accessed publicly, however only one instance will be there available through out its life time. In Java, Singleton Pattern can be easily implemented by making the constructor to private and provide … Continue reading “Singleton Design Pattern”
Spring Boot Application with JSP
Spring Boot Application is a stand-alone solution of a normal Spring Web Application. It comes with pre-configured conventions of the spring settings.Hence Spring Boot Application needs little configuration and configurations when there is a specific change. Features: Create Stand alone Spring Applications. Automatically include Spring configurations/load default configurations. In Memory Tomcat Web server for standalone … Continue reading “Spring Boot Application with JSP”
Design Patterns & Types
A design pattern is a template in which application can be designed so that it will provide us the tight cohesion and loose coupling of the components/classes, so that the application can be extended/modified very easily for further use. Designers gave us the templates so that the design can be picked up based on the … Continue reading “Design Patterns & Types”
Building JSON Object Java
A JSON Object can be created using Java very easily using any JSON library.We are using JSON-Simple.jar to build/parse the JSON Object using Java.
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 |
import org.json.simple.JSONObject; class JSONBuilder { public static void main(String[] args){ JSONObject jsonObject = new JSONObject(); jsonObject.put("names", "testjsonuser3"); jsonObject.put("action", "IllustrateJSONObjectInScript"); jsonObject.put("logincount", "3"); System.out.print(jsonObject); } } |
Results: {“names”:”testjsonuser3″,”actions”:”IllustrateJSONObjectInScript”,”logincount”:3} We could see the result displays the JSON Object., and it has string field printed with double quotes.The java Object is created with only name value pairs in … Continue reading “Building JSON Object Java”
JSON Object Conversion (Java1.4)
JSON-lib is one of the standard library used for converting JSON String to any Custom Object and also any Custom Object to JSON String. JSON-lib is one of the standard library used for converting JSON String to any Custom Object and also any Custom Object to JSON String. This library is compatible even for lower … Continue reading “JSON Object Conversion (Java1.4)”
Parsing JSON Object – Java
In Java, JSON object can be directly converted to String and we can do String manipulations. Also, the JSON Object created can be parsed using the field name present in JSON Object.
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 |
import org.json.simple.JSONObject; class JSONBuilder { public static void main(String[] args) { JSONObject jsonObject = new JSONObject(); jsonObject.put("names", "testjsonuser3"); jsonObject.put("actions", "IllustrateJSONObjectInScript"); jsonObject.put("logincount", "3"); System.out.print(jsonObject); StringWriter out = new StringWriter(); jsonObject.writeJSONString(out); String jsonString = out.toString(); System.out.println("JSON Converted to String:"); System.out.print(jsonString); System.out.println("JSONObject Values:"); System.out.print("names:" + jsonObject.get("names")); System.out.print(",actions:" + jsonObject.get("actions")); System.out.print(",logincount:" + jsonObject.get("logincount")); } } |
Result:
|
1 2 3 4 |
JSON Converted to String: {“names”: “testjsonuser3”, “actions”:”IllustrateJSONObjectInScript”, “logincount”:3} JSONObject Values: names:testjsonuser3,actions: IllustrateJSONObjectInScript,logincount: 3 |
The “names”, “actions” and “logincount” values is displayed by parsing the object jsonObject directly in java as explained.