Found 208 Articles for JSON

How to parse a JSON without duplicate keys using Gson in Java?

Aishwarya Naglot
Updated on 13-May-2025 15:46:49

1K+ Views

Parsing a JSON without duplicate keys means converting a JSON string into a Java object. But we need to keep in mind that the JSON string should not have duplicate keys. Gson: Parsing a JSON Without Duplicate Keys We can use the Gson library to parse a JSON without duplicate keys in Java. It is developed by Google and used for converting Java objects into JSON and vice versa. By default, Gson does not allow duplicate keys in JSON. If a JSON string has duplicate keys, Gson will throw a JsonSyntaxException. We will use the methods fromJson() and toJson() to achieve ... Read More

How can we format a date using the Jackson library in Java?

Aishwarya Naglot
Updated on 01-Sep-2025 13:36:31

3K+ Views

Jackson is a Java-based library, and it can be useful to convert 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 format a date using the setDateFormat() of ObjectMapper class. This method can be used for configuring the default DateFormat when serializing time values as Strings and deserializing from JSON Strings. Syntax of setDateFormat() public ObjectMapper setDateFormat(DateFormat df) Here, df is the DateFormat instance to be used for formatting dates. Steps to format a date using Jackson in Java Following ... Read More

How to serialize a map using the flexjson library in Java?

Aishwarya Naglot
Updated on 13-May-2025 15:54:20

378 Views

Serializing a Map in Java is the process of converting a Map object into a format that can be easily stored or transmitted, such as JSON or XML. We will use the Flexjson library to serialize a map in Java. We can serialize a Map using the serialize() method of the JSONSerializer class, which performs a shallow serialization of the target instance. Example of Serializing a Map We will take a map and see the output after serialization. Map map = new HashMap(); map.put("name", "Ansh"); map.put("age", "23"); map.put("city", "Mumbai"); map.put("country", "India"); After serialization, the map will look like this: {"name":"Ansh", ... Read More

How to define the naming conventions for JSON field names in Java?

raja
Updated on 14-Feb-2020 06:58:38

2K+ Views

The FieldNamingPolicy can be used to define a few standard naming conventions for JSON field names and it can be used in conjunction with GsonBuilder to configure a Gson instance to properly translate Java field names into the desired JSON field names. We can use the setFieldNamingPolicy() method of GsonBuilder to configure a specific naming policy strategy to an object's field during serialization and deserialization.Gson supports various field naming requirements with following field naming policiesFieldNamingPolicy.IDENTITY: It uses the exact same naming as the Java model when it serializes an object.FieldNamingPolicy.LOWER_CASE_WITH_UNDERSCORES: It modifies a Java field name from its camel-cased form to a lower case field name where ... Read More

How to exclude a field in Gson during serialization in Java?

Aishwarya Naglot
Updated on 13-May-2025 17:38:16

694 Views

Serialization is the process that converts an object into a format that can be stored or transmitted easily. Excluding a Field in Gson During Serialization We might have some scenarios where we are asked or required to exclude a certain field from the object while serializing it. For example, we have a Student object with fields like name, age, and address. We want to serialize the object but exclude the address field for building basic information about the student in this case, we use In this article, we will learn how to exclude a field in Gson during serialization ... Read More

Differences between fromJson() and toJson() methods of Gson in Java?

Aishwarya Naglot
Updated on 13-May-2025 16:23:07

5K+ Views

Gson is a Java library which is developed by Google and used to convert Java objects into JSON and vice versa. It is mostly used in applications where data can be exchanged in JSON format. There are two useful methods in Gson that we are going to discuss in this article. These methods are fromJson() and toJson(). The fromJson() Method The fromJson() method is used to convert a JSON string into a Java object. It takes two parameters: the JSON string and the class type of the object you want to create. Here is the syntax of the fromJson() method: ... Read More

How can we serialize an array of objects using flexjson in Java?

Aishwarya Naglot
Updated on 13-May-2025 16:28:51

448 Views

Serialization is the process where we convert an object into a format that can be easily stored or transmitted and later reconstructed. The given task is to serialize an array of objects using Flexjson in Java. Serializing an Array of Objects Using Flexjson Flexjson is a lightweight Java library for serializing and deserializing java beans, maps, arrays, and collections in JSON format. We can also deserialize a JSON string to an existing object using the deserializeInto() method of the JSONDeserializer class, this method deserializes the given input into the existing object target. The values in the JSON input can overwrite ... Read More

How to deserialize a JSON into an existing object in Java?

Aishwarya Naglot
Updated on 13-May-2025 16:33:52

3K+ Views

Deserialization is the process of converting a JSON string into a Java object. In this example, we will learn how to deserialize a JSON string into an existing object in Java. There are libraries available in Java that can help us to deserialize a JSON string into an existing object. Some of the popular libraries are: Using Gson Using Jackson Using Flexjson Let's learn how to use each of these libraries to deserialize a JSON string into an existing object in Java. Deserializing JSON Object Using ... Read More

How to exclude a field from JSON using @Expose annotation in Java?

Aishwarya Naglot
Updated on 12-May-2025 13:09:12

3K+ Views

Sometimes we need to exclude certain fields from the JSON output. For example, if we have a field that contains sensitive information, we may not want to include it in the JSON representation of the object. In such cases, we can use the Expose annotation provided by Gson. The Gson @Expose annotation The Gson @Expose annotation can be used to specify whether a field isto be exposed or not (included or not). The @Expose annotation can take two parameters, and each parameter is a boolean which can take either the value true or false. The two parameters are: ... Read More

How can we read & write a file using Gson streaming API in Java?

Aishwarya Naglot
Updated on 12-May-2025 13:06:24

1K+ Views

We can read and write a file using Gson streaming API, and it is based on the sequential read and write standard. The JsonWriter and JsonReader are the core classes built for streaming write and read in Streaming API. Reading & Writing a File using Gson Streaming API The JsonWriter writes a JSON-encoded value to a stream, one token at a time. The stream includes both literal values (strings, numbers, booleans, and nulls) as well as begin and end delimiters of objects and arrays, and JsonReader reads a JSON-encoded value as a stream of tokens. The tokens are traversed in depth-first order, ... Read More

Advertisements