Found 208 Articles for JSON

How to auto-increment the property of a JSONObject in Java?

raja
Updated on 06-Jul-2020 13:03:45

1K+ Views

A JSONObject is an unordered collection of name/value pairs and parses text from a String to produce a map-like object. However, we can auto-increment the property of a JSONObject using the increment() method of JSONObject class. If there is no such property, create one with a value of 1. If there is such a property and if it is an Integer, Long, Double or Float then add one to it.Syntaxpublic JSONObject increment(java.lang.String key) throws JSONExceptionExampleimport org.json.JSONException; import org.json.JSONObject; public class IncrementJSONObjectTest {    public static void main(String[] args) throws JSONException {       JSONObject jsonObj = new JSONObject();       jsonObj.put("year", 2019);     ... Read More

Convert JSONObject to/from a cookie in Java?

raja
Updated on 06-Jul-2020 13:05:15

1K+ Views

The JSON is one of the widely used data-interchange formats and is a lightweight and language independent. We can convert a JSONObject to cookie using the toString() method and convert a cookie to JSONObject using the toJSONObject() method of org.json.Cookie class.Convert JSONObject to cookieSyntaxpublic static java.lang.String toString(JSONObject jo) throws JSONExceptionExampleimport org.json.Cookie; import org.json.JSONObject; public class JSONObjectToCookieTest {    public static void main(String args[]) {       JSONObject jsonObject = new JSONObject();       jsonObject.put("path", "/");       jsonObject.put("expires", "Thu, 07 May 2020 12:00:00 UTC");       jsonObject.put("name", "username");       jsonObject.put("value", "Adithya");       String cookie = Cookie.toString(jsonObject); ... Read More

How to convert a JSON to Java Object using the Jackson library in Java?

Manisha Chand
Updated on 19-May-2025 15:51:31

42K+ Views

Jackson is a Java library that is used to convert JSON to Java objects and vice versa. Conversion of JSON to a Java object is called deserialization, and Java object to JSON is known as serialization. Both of these tasks can be done by using the Jackson library. In this article, we are going to learn how to convert JSON to Java objects using the Jackson library. Jackson Library: Convert a JSON to a Java Object The ObjectMapper class belongs to the Jackson library. This class is responsible for the serialization and deserialization of Java objects. The ObjectMapper class is used ... Read More

How to wrap a JSON using flexjson in Java?

raja
Updated on 06-Jul-2020 12:45:15

1K+ Views

The Flexjson library is a lightweight Java library for serializing and de-serializing java beans, maps, arrays, and collections in a JSON format. A JSONSerializer is the main class for performing serialization of Java objects to JSON and by default performs a shallow serialization. We can wrap a JSON object using the rootName() method of JSONSerializer class, this method wraps the resulting JSON in a javascript object that contains a single field named rootName.Syntaxpublic JSONSerializer rootName(String rootName)Exampleimport flexjson.JSONSerializer; public class JSONRootNameTest {    public static void main(String[] args) {       JSONSerializer serializer = new JSONSerializer().rootName("My_Employee").prettyPrint(true);       Employee emp = new Employee("Adithya", "Jai", 28, ... Read More

How can we convert a JSONArray to String Array in Java?

Manisha Chand
Updated on 06-Jun-2025 12:43:59

3K+ Views

A JSONArray is a class provided by the org.json package that represents a collection of JSON values. These values can be of any type, such as strings, numbers, booleans, or even nested objects or arrays. If you do not know what JSON is, then you can read the JSON tutorial. Converting JSON Array to String ArrayWe can convert a JSONArray to String Array by using a simple loop as shown in the example below - import org.json.*; import java.util.*; public class JsonArraytoStringArrayTest { public static void main(String[] args) { ... Read More

How to convert a List to JSON array using the Jackson library in Java?

Manisha Chand
Updated on 29-Apr-2025 12:24:45

11K+ Views

JSON is used in Java applications in APIs, file storage, and data communication between systems. Sometimes, we need to convert a list into a JSON array. In this article, we will learn how to convert a list to JSON array using the Jackson library. Jackson Library It is a library that is used in Java to work with JSON data. It provides APIs to serialize Java objects into JSON and deserialize JSON back into Java objects. If you want to read more about the Jackson library, you can refer Jackson library. There are mainly three components of Jackson - ... Read More

How can we map multiple date formats using Jackson in Java?

Manisha Chand
Updated on 21-Apr-2025 16:18:18

3K+ Views

When we are working with those Java applications that handle JSON data, it is common that we find different date formats. It might happen that one field contains just the date, while another includes time and date, and even time zone. For handling this type of data Jackson library is very helpful. Jackson is a popular Java library that is used to convert Java objects into JSON and vice versa. And this process is called serialization and deserialization. To handle multiple date formats in a single class, Jackson provides an annotation called @JsonFormat. The@JsonFormat The @JsonFormat annotation helps Jackson ... Read More

How can we convert a JSON array to a list using Jackson in Java?

Aishwarya Naglot
Updated on 05-Jun-2025 14:58:10

9K+ Views

Jackson is a Java-based library, and it can be useful for converting Java objects to JSON and JSON to Java objects. A Jackson API is faster than other API, needs less memory, and is good for large objects. We can convert a JSON array to a list using the ObjectMapper class. It has a useful method,  readValue(), which takes a JSON string and converts it to the object class specified in the second argument. Before proceeding further, first of all, we need to import the Jackson library into our project. To do this, you can add the following dependency to ... Read More

How can we serialize a list of objects using flexjson in Java?

raja
Updated on 06-Jul-2020 12:26:47

1K+ Views

The Flexjson is a lightweight library for serializing and deserializing Java objects into and from JSON format. We can serialize a list of objects using the serialize() method of JSONSerializer class. This method can perform a shallow serialization of the target instance. We need to pass a list of objects of List type as an argument to the serialize() method.Syntaxpublic String serialize(Object target)Exampleimport flexjson.JSONSerializer; import java.util.*; public class JsonSerializeListTest {    public static void main(String[] args) {       JSONSerializer serializer = new JSONSerializer().prettyPrint(true); // pretty print JSON       Student s1 = new Student("Raja", "Ramesh", 28, "Hyderabad");       Student s2 = new Student("Suresh", "Kumar", 30, "Chennai");   ... Read More

How can we create a JSON using JsonGenerator in Java?

Aishwarya Naglot
Updated on 13-May-2025 15:43:25

7K+ Views

JSON is a format for storing and exchanging data. It is easily readable and also easy to parse. Creating JSON Using Java JsonGenerator In Java, we can create a JSON object using the JsonGenerator class. JsonGenerator is class in the javax.json.stream package that is used to create JSON data. It provides methods to write JSON objects, arrays, and values. To use the JsonGenerator class, we need to add the javax.json library to our project. If you are using Maven, add this to your pom.xml file: javax.json javax.json-api 1.1.4 If you are not using Maven, you can download ... Read More

Advertisements