Raja has Published 469 Articles

How to add elements to JSON Object using JSON-lib API in Java?

raja

raja

Updated on 08-Jul-2020 07:31:03

3K+ Views

The JSON-lib is a Java library for serializing and de-serializing java beans, maps, arrays, and collections in JSON format. We can add elements to the JSON object using the element() method of JSONObject class. We need to download all the dependent jars like json-lib.jar, ezmorph.jar, commons-lang.jar, commons-collections.jar, commons-beanutils.jar, and commons-logging.jar and can import net.sf.json ... Read More

How to convert a Collection to JSON Array using JSON-lib API in Java?

raja

raja

Updated on 08-Jul-2020 07:30:31

876 Views

The net.sf.json.JSONArray is an ordered sequence of values. Its external text form is a string wrapped in square brackets with commas separating the values and an internal form is an object having get() and opt() methods for accessing the values by index, and element() method for adding or replacing values. The ... Read More

How to serialize and deserialize a JSON using the ExclusionStrategy interface in Java?

raja

raja

Updated on 08-Jul-2020 06:48:57

713 Views

The ExclusionStrategy interface can be used to exclude any field during serialization and deserialization. We can provide a custom implementation of the ExclusionStrategy interface and need to register it with GsonBuilder using the setExclusionStrategies() method. It configures Gson to apply a set of exclusion strategies during serialization and deserialization.Syntaxpublic GsonBuilder setExclusionStrategies(ExclusionStrategy... strategies)Exampleimport com.google.gson.*; import ... Read More

How can we update an existing JSON data using javax.json API in Java?

raja

raja

Updated on 08-Jul-2020 06:48:13

7K+ Views

The JsonBuilderFactory interface is a factory to create JsonObjectBuilder instance and JsonObjectBuilder is a builder for creating JsonObject models from scratch. This interface initializes an empty JSON object model and provides methods to add name/value pairs to the object model and to return the resulting object. We can create a JsonObjectBuilder instance that can be used ... Read More

JSON Schema Support using Jackson in Java?

raja

raja

Updated on 08-Jul-2020 06:46:35

5K+ Views

JSON Schema is a specification for JSON based format for defining the structure of JSON data. The JsonSchema class can provide a contract for what JSON data is required for a given application and how to interact with it. The JsonSchema can define validation, documentation, hyperlink navigation, and interaction control of JSON ... Read More

How to convert Java array or ArrayList to JsonArray using Gson in Java?

raja

raja

Updated on 08-Jul-2020 06:20:07

7K+ Views

The Java Arrays are objects which store multiple variables of the same type, it holds primitive types and object references and an ArrayList can represent a resizable list of objects. We can add, remove, find, sort and replace elements using the list. A JsonArray can parse text from a string to produce a ... Read More

How to resolve "Expected BEGIN_OBJECT but was BEGIN_ARRAY" using Gson in Java?

raja

raja

Updated on 08-Jul-2020 05:49:52

18K+ Views

While deserializing, a Gson can expect a JSON object but it can find a JSON array. Since it can't convert from one to the other, it can throw an error as "JsonSyntaxException: java.lang.IllegalStateException: Expected BEGIN_OBJECT but was BEGIN_ARRAY" at the runtime.Exampleimport com.google.gson.Gson; public class GsonErrorTest {    public static void ... Read More

How to serialize a JSON object with JsonWriter using Object Model in Java?

raja

raja

Updated on 08-Jul-2020 05:40:56

3K+ Views

The javax.json.JsonWriter interface can write a JSON object or array structure to an output source. The class javax.json.JsonWriterFactory contains methods to create JsonWriter instances. A factory instance can be used to create multiple writer instances with the same configuration. We can create writers from output source using the static method createWriter() of javax.json.Json class.Syntaxpublic static JsonWriter createWriter(Writer ... Read More

How to handle the errors generated while deserializing a JSON in Java?

raja

raja

Updated on 07-Jul-2020 13:18:11

3K+ Views

The DeserializationProblemHandler class can be registered to get called when a potentially recoverable problem is encountered during the deserialization process. We can handle the errors generated while deserializing the JSON by implementing the handleUnknownProperty() method of DeserializationProblemHandler class.Syntaxpublic boolean handleUnknownProperty(DeserializationContext ctxt, JsonParser p, JsonDeserializer deserializer, Object beanOrClass, String propertyName) throws IOExceptionExampleimport java.io.*; ... Read More

How to merge two JSON strings in an order using JsonParserSequence in Java?

raja

raja

Updated on 07-Jul-2020 13:06:13

863 Views

The JsonParserSequence is a helper class that can be used to create a parser containing two sub-parsers placed in a particular sequence. We can create a sequence using the static method createFlattened() of the JsonParserSequence class.Syntaxpublic static JsonParserSequence createFlattened(JsonParser first, JsonParser second)Exampleimport java.io.*; import com.fasterxml.jackson.core.*; import com.fasterxml.jackson.core.util.*; public class JsonParserSequenceTest {    public static void main(String[] ... Read More

Advertisements