Found 2616 Articles for Java

How to construct a JSON object from a subset of another JSON object in Java?

raja
Updated on 04-Jul-2020 05:50:07

3K+ Views

The JSON stands for JavaScript Object Notation and it can be used to transfer and storage of data. The JSONObject can parse text from a String to produce a map-like object. We can also construct a JSON object from a subset of another JSON object using the JSONObject(JSONObject jo, java.lang.String[] names) constructor, an array of strings is used to identify the keys that can be copied and missing keys are ignored.Syntaxpublic JSONObject(JSONObject jo, java.lang.String[] names)Exampleimport java.util.*; import org.json.*; public class JSONSubsetTest {    public static void main(String[] args) throws JSONException {       Map map = new HashMap();     ... Read More

How can we merge two JSON arrays in Java?

raja
Updated on 04-Jul-2020 05:51:07

6K+ Views

A JSON is a lightweight data-interchange format and the format of JSON is a key with value pair. The JSONArray can parse text from a String to produce a vector-like object and supports java.util.List interface. We can use org.json.simple.JSONArray class to merge two JSON arrays in Java.We can merge two JSON arrays using the addAll() method (inherited from interface java.util.List) in the below program.Exampleimport org.json.simple.JSONArray; import java.io.IOException; public class MergeJSONArraysTest {    public static void main(String[] args) throws IOException {       JSONArray jsonArray1 = new JSONArray(); // first json array       jsonArray1.add("Java");       jsonArray1.add("Python");       jsonArray1.add("Spark");     ... Read More

How can we convert a map to the JSON object in Java?

raja
Updated on 04-Jul-2020 05:51:35

9K+ Views

The JSON is a lightweight, text-based and language-independent data exchange format. The JSON can represent two structured types like objects and arrays. An object is an unordered collection of key/value pairs and an array is an ordered sequence of values. We can convert a Map to JSON object using the toJSONString() method(static) of org.json.simple.JSONValue. It has two important static methods: writeJSONString() method to encode an object into JSON text and write it out, escape() method to escape the special characters and escape quotes,  \, /, \r, , \b, \f, \t.Exampleimport java.util.*; import org.json.simple.JSONValue; public class ConvertMapJSONTest {    public static void main(String[] args) {       ... Read More

How can we merge two JSON objects in Java?

raja
Updated on 04-Jul-2020 05:40:36

15K+ Views

A JSON is a lightweight data-interchange format and the format of JSON is a key with value pair. The JSONObject can parse a text from a String to produce a map-like object and supports java.util.Map interface. We can use org.json.simple.JSONObject to merge two JSON objects in Java.We can merge two JSON objects using the putAll() method (inherited from interface java.util.Map) in the below program.Exampleimport java.util.Date; import org.json.simple.JSONObject; public class MergeJsonObjectsTest {    public static void main(String[] args) {       JSONObject jsonObj = new JSONObject(); // first json object       jsonObj.put("Name", "Adithya");       jsonObj.put("Age", 25);     ... Read More

How can we write JSON objects to a file in Java?

raja
Updated on 04-Jul-2020 05:35:31

2K+ Views

The JSON is one of the widely used data-interchange formats and is a lightweight and language independent. The json.simple is a lightweight JSON processing library that can be used to write JSON files and it can be used to encode or decode JSON text and fully compliant with JSON specification(RFC4627). In order to read a JSON file, we need to download the json-simple.jar file and set the path to execute it.Exampleimport java.io.*; import java.util.*; import org.json.simple.*; import org.json.simple.parser.*; public class JSONObjectWriterToFileTest {    public static void main(String[] args) throws IOException {       JSONObject obj = new JSONObject();       obj.put("Name", "Adithya");       ... Read More

How can we add a JSONArray to JSONObject in Java?

raja
Updated on 04-Jul-2020 05:36:04

16K+ Views

The JSON is a text-based format for exchanging data. It is a lightweight component and language independent. We can also add a JSONArray to JSONObject. We need to add a few items to an ArrayList first and pass this list to the put() method of JSONArray class and finally add this array to JSONObject using the put() method. Exampleimport org.json.*; import java.util.*; public class AddJSONArrayToJSONObjTest {    public static void main(String args[]) {       List list = new ArrayList();       list.add("Raja");       list.add("Jai");       list.add("Adithya");       JSONArray array = new JSONArray();       for(int i = 0; i < list.size(); ... Read More

How can we read a JSON file in Java?

raja
Updated on 02-Sep-2023 12:55:51

54K+ Views

The JSON is one of the widely used data-interchange formats and is a lightweight and language independent. The json.simple is a lightweight JSON processing library that can be used to read and write JSON files and it can be used to encode or decode JSON text and fully compliant with JSON specification (RFC4627). In order to read a JSON file, we need to download the json-simple.jar file and set the path to execute it.json fileExampleimport java.io.*; import java.util.*; import org.json.simple.*; import org.json.simple.parser.*; public class JSONReadFromTheFileTest {    public static void main(String[] args) {       JSONParser parser = new JSONParser();       try {     ... Read More

Difference between ArrayList and HashSet in Java

Nitin Sharma
Updated on 18-Sep-2019 11:35:45

13K+ Views

HashSet and ArrayList both are some of the most important classes of the Java Collection framework.The following are the important differences between ArrayList and HashSet.Sr. No.KeyArrayListHashSet1ImplementationArrayList is the implementation of the list interface.HashSet on the other hand is the implementation of a set interface.2Internal implementationArrayList internally implements array for its implementation.HashSet internally uses Hashmap for its implementation.3Order of elementsArrayList maintains the insertion order i.e order of the object in which they are inserted.HashSet is an unordered collection and doesn't maintain any order.4DuplicatesArrayList allows duplicate values in its collection.On other hand duplicate elements are not allowed in Hashset.5Index performanceArrayList uses index ... Read More

Difference between Applets and Servlets in Java.

Nitin Sharma
Updated on 16-Sep-2019 11:39:37

4K+ Views

In java, both Applets and servlets are the programs or applications that run in a java environment. The main difference in both the programs is in their processing is done in different environments.The following are the important differences between Applets and Servlets.Sr. No.KeyAppletsServlets1ExecutionApplets are executed on client-side i.e applet runs within a Web browser on the client machine.Servlets on other hand executed on the server-side i.e servlet runs on the web Page on server.2Parent packagesParent package of Applet includes java.applet.* and java.awt.*Parent package of Servlet includes javax.servlet.* and java.servlet.http.*3MethodsImportant methods of applet includes init(), stop(), paint(), start(), destroy().Lifecycle methods of ... Read More

Difference between an Iterator and ListIterator in Java

Nitin Sharma
Updated on 16-Sep-2019 11:31:47

734 Views

Java provided these two interfaces to traverse the data one by one stored in a collection. The internal implementation of iterator and list iterator makes them differ apart but the main agenda of both the iterators is the same.The following are the important differences between Iterator and ListIterator.Sr. No.KeyIteratorListIterator1ApplicableIterator can be used to traverse any collection irrespective of the type of collection.List iterator can only be used to iterate only List collection implemented classes like arraylist, linkedlist etc.2CallingAs mentioned Iterator must be used to enumerate elements in all Collections implemented interfaces like Set, List, Queue, Deque and also in all ... Read More

Advertisements