Found 7442 Articles for Java

How can we encode a JSON object in Java?

raja
Updated on 04-Jul-2020 05:58:28

3K+ Views

A JSONObject is a subclass of java.util.HashMap where no order is provided. We can also use the strict ordering of elements as well with the help of the JSONValue.toJSONString(map) method i.e. by the implementation of java.util.LinkedHashMap.We can encode a JSON object in the below two examples.Example import java.util.*; import org.json.simple.JSONObject; public class JSONEncodingTest {    public static void main(String[] args) {       Map dataMap = new HashMap();       dataMap.put("Name", "Adithya");       dataMap.put("Age", new Integer(25));       dataMap.put("Salary", new Double(25000.00));       dataMap.put("Employee Id", new Integer(115));       dataMap.put("Company", "TutorialsPoint");       JSONObject ... Read More

How can we parse a nested JSON object in Java?

Aishwarya Naglot
Updated on 22-Apr-2025 16:28:52

15K+ Views

Sometimes, we need to parse a nested JSON object, which means an object inside another object. In this article, let's learn about how to parse a nested JSON object in Java. If you don't know about JSON, refer JSON overview. Parsing a Nested JSON Object in Java? We can achieve this using some of the popular Java libraries. Those are listed below: Using org.json library : We will use jsonObject.getJSONObject method to parse a nested JSON object. Using Gson library : We will use JsonParser.parseString(json).getAsJsonObject(). Using Jackson library : In this, We will use ObjectMapper.readTree(json) method. Now, ... Read More

Convert a JSON object to XML format in Java?

Aishwarya Naglot
Updated on 21-Apr-2025 18:54:40

7K+ Views

In this article, we will learn how to convert a JSON object into XML format in Java. But first, let’s understand what JSON and XML are. JSON is a lightweight data-interchange format, and the format of JSON is like a key-value pair. XML (Extensible Markup Language) is also a way to store data, but unlike JSON, it uses tags to make the proper structure. Why Do we Convert JSON to XML? There can be multiple situations where we would need to convert JSON to XML. Here are some of the reasons: XML is ... Read More

Convert a JSON String to Java Object using the json-simple library in Java?

Aishwarya Naglot
Updated on 21-Apr-2025 19:35:29

2K+ Views

In this article, we will learn how to convert a JSON string to a Java object using the json.simple library. JSON stands for JavaScript Object Notation. It's a simple format that stores data in key-value pairs. It is used for sharing data between a server and a web app. To learn more about JSON, you can refer to the JSON tutorial. In Java, we can use a library called json.simple to handle JSON data. So, using this, we can easily convert a JSON string into a Java object. Why Convert JSON String to Java Object? There are many reasons to ... Read More

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

Aishwarya Naglot
Updated on 21-Apr-2025 19:33:12

3K+ Views

In this article, we will learn how to construct a JSON object from a subset of another JSON object in Java. If you are not familiar with JSON and its usage in Java, you can refer to our JSON Overview tutorial. JSON Object From a Subset of Another Object There is more than one way to construct a JSON object from a subset of another JSON object in Java. Some of the popular libraries that we can use are - Using org.json library Using Gson library Using ... Read More

How can we merge two JSON arrays in Java?

Aishwarya Naglot
Updated on 22-Apr-2025 18:35:33

8K+ Views

In this article, we will be learning how to merge two JSON arrays in Java. Let's understand step by step. First, if you are not familiar with JSON, refer JSON overview. How to Merge Two JSON Arrays in Java? There are multiple ways to merge two JSON arrays in Java. Some of them are: Using org.json library: We will use JSONArray.put() method to merge two JSON arrays. Using Gson library: We will use JsonArray.add() method to merge two JSON arrays. Using Jackson library: We will use ObjectMapper.createArrayNode() ... 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

Difference between Arrays and Collection in Java

Nitin Sharma
Updated on 17-Sep-2019 10:34:05

14K+ Views

In order to store multiple values or objects of the same type, Java provides two types of data structures namely Array and Collection.The following are the important differences between Arrays and Collection.Sr. No.KeyArraysCollection1SizeArrays are fixed in size i.e once the array with the specific size is declared then we can't alter its size afterward.The collection is dynamic in size i.e based on requirement size could be get altered even after its declaration.2Memory ConsumptionArrays due to fast execution consumes more memory and has better performance.Collections, on the other hand, consume less memory but also have low performance as compared to Arrays.3Data ... Read More

How can we merge two JSON objects in Java?

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

17K+ 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

Advertisements