Java Articles

Page 157 of 450

How can we sort a JSONArray in Java?

Aishwarya Naglot
Aishwarya Naglot
Updated on 22-Apr-2025 10K+ Views

In this article, we will learn how to sort a JSON array in Java. But first, let’s understand what JSON is. If you do not have knowledge about JSON, you can refer to the tutorial JSON Overview. Now, let's see different ways to sort a JSON array in Java. Ways to sort a JSON array in Java Following are different ways to sort a JSON array in Java - Sort JSON array Without any Dependency Using org.json library: Methods are getJSONArray() and getJSONObject() Using Gson library: Methods ...

Read More

How to get the values of the different types from a JSON object in Java?

Aishwarya Naglot
Aishwarya Naglot
Updated on 22-Apr-2025 31K+ Views

We often need to extract values from a JSON object. In this article, we will learn how to get the values of the different types from a JSON object in Java. What is JSONObject? Java JSONObject is a class in the org.json package that represents a JSON object. Refer JSONObject for more information. Let's see the steps to get the values of different types from a JSON object in Java. Getting different values from a JSON object Before we start, let's add the required library to our program. We can add a .jar file, or we can use a Maven ...

Read More

Convert a Map to JSON using the Gson library in Java?

Aishwarya Naglot
Aishwarya Naglot
Updated on 22-Apr-2025 4K+ Views

Converting a map to a JSON object is our task here. Let's see how to convert a map to a JSON object in Java using the Gson library. Converting a Map to JSON using the Gson library JSON is a simple and lightweight data interchange format. It stores data in key-value pairs. A map is also a collection of key-value pairs. So, we can easily convert a map to a JSON object. If you are not familiar with JSON, refer JSON. And if you want to know more about Map, refer Map. Gson is developed by Google. It is an ...

Read More

How can we check if a JSON object is empty or not in Java?

Aishwarya Naglot
Aishwarya Naglot
Updated on 22-Apr-2025 12K+ Views

In this article, let's learn how to check if a JSON object has any values or not in Java. If you don't know about JSON, refer JSON. We will use the org.json library and its methods like isEmpty(), length(), and keys() to check if a JSON object is empty or not. We can check if a JSON object is empty or not using the following methods: Using isEmpty() method Using length() method Using keys() method Let's see how to use each of them one by one. ...

Read More

How to convert a JSON array to CSV in Java?

Aishwarya Naglot
Aishwarya Naglot
Updated on 22-Apr-2025 6K+ Views

JSON is a lightweight data interchange format. It is mostly used in web applications for sending and receiving data. To know more about JSON, refer JSON. A JSON array is a collection of JSON objects. It is similar to a list in Java. CSV is the format for storing tabular data in plain text. It is a comma-separated value. It is used for data exchange between applications. Let's see how CSV format looks like: JSON array The following is an example of the JSON array: [ { "Name": "Ansh", ...

Read More

When can we use a JSONStringer in Java?

Aishwarya Naglot
Aishwarya Naglot
Updated on 22-Apr-2025 599 Views

A JSONStringer provides a convenient way of producing JSON text, and it can strictly follow to JSON syntax rules. Each instance of JSONStringer can produce one JSON text. A JSONStringer instance provides a value method for appending values to the text and a key-method for adding keys before values in objects. We have array () and endArray() methods that create and bind array values, and object() and endObject () methods that create and bind object values. It is part of the org.json library. It is generally used in Android and lightweight Java programs. In this article, we will learn when to use a JSONStringer in Java. When to use ...

Read More

How to pretty print JSON using the Gson library in Java?

Aishwarya Naglot
Aishwarya Naglot
Updated on 22-Apr-2025 4K+ Views

JSON is a data interchange format that is easy to read and write. It is lightweight and gets parsed easily. It is commonly used in web applications for sending and receiving data. To know more about JSON, refer to JSON. Pretty print is a type of formatting that formats data in a more easily readable way by adding indentation and line breaks. It is useful for debugging and logging purposes. Enabling pretty print Using Gson Library Gson is a JSON library for Java, which was created by Google. By using Gson, we can generate JSON and convert JSON to Java objects. ...

Read More

How to Sort a String in Java alphabetically in Java?

Maruthi Krishna
Maruthi Krishna
Updated on 22-Apr-2025 92K+ Views

Sorting a string alphabetically means rearranging its characters in order, from A to Z. For example, the string "java" becomes "aajv" after sorting. Different ways to sort a String Alphabetically There are two ways to do this in Java using different approaches - Using toCharArray() and Arrays.sort() Method Sorting the array manually Using toCharArray() and Arrays.sort() Method The toCharArray() method of this ...

Read More

In how many ways you can retrieve the elements of a collection in Java?

Maruthi Krishna
Maruthi Krishna
Updated on 22-Apr-2025 3K+ Views

In Java, the collections framework provides various classes like ArrayList, HashSet, and LinkedList to store groups of elements. But once data is stored, how do you access or retrieve it? Java provides multiple ways to retrieve elements from collections, depending on whether you're reading values, modifying them during traversal, or iterating forward or backward. You can retrieve the elements of a collection in four ways- Using For-Loop Using For-Each Loop ...

Read More

How many ways are there to convert an Array to ArrayList in Java?

Maruthi Krishna
Maruthi Krishna
Updated on 22-Apr-2025 468 Views

When working with collections in Java, you might need to convert an array to an ArrayList. This is useful because ArrayList offers more flexibility, such as dynamic sizing and built-in methods for adding, removing, and searching elements. Need for Converting an Array to an ArrayList Arrays in Java have a fixed size and provide limited functionality. ArrayList, on the other hand, is part of the Java Collections Framework and provides methods to modify data dynamically. It is useful when - Need to use dynamic Collections. ...

Read More
Showing 1561–1570 of 4,498 articles
« Prev 1 155 156 157 158 159 450 Next »
Advertisements