JSON Articles

Page 12 of 16

How to get the values of a key using the JsonPointer interface in Java?

raja
raja
Updated on 07-Jul-2020 867 Views

The JSONPointer is a standard that defines a string syntax that can be used to access a particular key value in the JSON document. An instance of JSONPointer can be created by calling the static factory method createPointer() on the Json class. In the JSONPointer,  every string syntax is prefixed with “/”. We can get the value of a key by calling the getValue() method on the JsonPointer object.JSON fileExampleimport javax.json.*; import java.io.*; public class JsonPointerTest {    public static void main(String[] args) throws Exception {       JsonReader jsonReader = Json.createReader(new FileReader("simple.json"));       JsonStructure jsonStructure = jsonReader.read();       JsonPointer jsonPointer1 ...

Read More

Importance of the JsonPatch interface in Java?

raja
raja
Updated on 07-Jul-2020 671 Views

The JsonPatch interface is a format for storing a sequence of operations that can be applied to the target JSON structure. There are few operations like add, remove, replace, copy, move and test can be stored in JsonPath and operated on JSON structure. The JsonPatchBuilder interface can be used for constructing a JSON patch using the Json.createPatchBuilder().JSON fileExampleimport java.io.*; import javax.json.Json; import javax.json.JsonPatch; import javax.json.JsonPatchBuilder; import javax.json.JsonReader; import javax.json.JsonStructure; public class JsonPatchTest {    public static void main(String[] args) throws Exception {       JsonPatchBuilder jsonPatchBuilder = Json.createPatchBuilder();       JsonPatch jsonPatch = jsonPatchBuilder.add("/postalCode", "500072").remove("/age").build();       JsonReader reader = Json.createReader(new FileReader("simple.json"));     ...

Read More

How to create a JSON using JsonObjectBuilder and JsonArrayBuilder in Java?

raja
raja
Updated on 07-Jul-2020 10K+ Views

The JsonObjectBuilder can be used for creating JsonObject models whereas the JsonArrayBuilder can be used for creating JsonArray models. The JsonObjectBuilder can be created using the Json class, it contains methods to create the builder object and build an empty JsonObject instance using the Json.createObjectBuilder().build(). The JsonArrayBuilder can be created using the Json class, it contains methods to create the builder object and build an empty JsonArray instance using Json.createArrayBuilder().build().Exampleimport java.io.*; import javax.json.*; public class JsonObjectTest {    public static void main(String[] args) {       JsonObject empObject = Json.createObjectBuilder().add("empName", "Jai")                                  .add("empAge", ...

Read More

How to parse a JSON string using Streaming API in Java?

raja
raja
Updated on 07-Jul-2020 3K+ Views

The Streaming API consists of an important interface JsonParser and this interface contains methods to parse JSON in a streaming way and provides forward, read-only access to JSON data. The Json class contains the methods to create parsers from input sources. We can parse a JSON using the static method createParser() of Json class.Syntaxpublic static JsonParser createParser(Reader reader)Exampleimport java.io.*; import javax.json.Json; import javax.json.stream.JsonParser; import javax.json.stream.JsonParser.Event; public class JSONParseringTest {    public static void main(String[] args) {       String jsonString = "{\"name\":\"Adithya\", \"employeeId\":\"115\", \"age\":\"30\"}";       JsonParser parser = Json.createParser(new StringReader(jsonString));       while(parser.hasNext()) {          Event event = ...

Read More

How to create a JSON Object using Object Model in Java?

raja
raja
Updated on 07-Jul-2020 3K+ Views

The javax.json.JsonObject interface can represent an immutable JSON object value and provides an unmodifiable map view to the JSON object name/value mappings. A JsonObject instance can be created from an input source using the static method readObject() of javax.json.JsonReader class and also can be created using the static method createObjectBuilder() of javax.json.Json class.Syntaxpublic static JsonObjectBuilder createObjectBuilder()Exampleimport java.io.*; import javax.json.*; public class JsonObjectTest {    public static void main(String[] args) throws Exception {       JsonObjectBuilder builder = Json.createObjectBuilder();       builder.add("Name", "Adithya");       builder.add("Designation", "Python Developer");       builder.add("Company", "TutorialsPoint");       builder.add("Location", "Hyderabad");       JsonObject data = builder.build();     ...

Read More

Importance of the accumulate() method of JSONObject in Java?

raja
raja
Updated on 07-Jul-2020 2K+ Views

A JSONObject is an unordered collection of a name and value pairs. A few important methods of JSONArray are accumulate(), put(), opt(), append(), write() and etc. The accumulate() method accumulates the values under a key and this method similar to the put() method except if there is an existing object stored under a key then a JSONArray can be stored under a key to hold all of the accumulated values. If there is an existing JSONArray then a new value can be added.Syntaxpublic JSONObject accumulate(java.lang.String key, java.lang.Object value) throws JSONExceptionExampleimport org.json.*; public class JSONAccumulateMethodTest {    public static void main(String[] args) throws JSONException { ...

Read More

How to convert a JSON object to an enum using Jackson in Java?

raja
raja
Updated on 07-Jul-2020 5K+ Views

A JSONObject can parse the text from String to produce a Map kind of an object. An Enum can be used to define a collection of constants, when we need a predefined list of values which do not represent some kind of numeric or textual data then we can use an enum. We can convert a JSON object to an enum using the readValue() method of ObjectMapper class.In the below example, we can convert/deserialize a JSON object to Java enum using the Jackson library.Exampleimport com.fasterxml.jackson.databind.*; public class JSONToEnumTest {    public static void main(String arg[]) throws Exception {       ObjectMapper mapper ...

Read More

What is the use of @JacksonInject annotation using Jackson in Java?\\n

raja
raja
Updated on 06-Jul-2020 690 Views

The Jackson @JacksonInject annotation can be used to inject the values into parsed objects instead of reading those values from the JSON. In order to inject values into a field, we can use the InjectableValues class and need to configure the ObjectMapper class to read both the injected values from the InjectableValues class and the remaining values from the JSON string.Syntax@Target(value={ANNOTATION_TYPE, METHOD, FIELD, PARAMETER}) @Retention(value=RUNTIME) public @interface JacksonInjectExampleimport com.fasterxml.jackson.annotation.*; import com.fasterxml.jackson.databind.*; import java.io.*; public class JacksonInjectTest {    public static void main(String args[]) throws IOException {       String jsonString = "{\"empName\": \"Raja Ramesh\"}";       InjectableValues injectableValues = new InjectableValues.Std().addValue(int.class, 110); ...

Read More

How to ignore a class during the serialization using Jackson in Java?

raja
raja
Updated on 06-Jul-2020 3K+ Views

The Jackson @JsonIgnoreType annotation can be used to ignore a class during the serialization process and it can mark all the properties or fields of a class to be ignored while serializing and deserializing a JSON object.Syntax@Target(value={ANNOTATION_TYPE, TYPE}) @Retention(value=RUNTIME) public @interface JsonIgnoreTypeExampleimport com.fasterxml.jackson.annotation.*; import com.fasterxml.jackson.core.*; import com.fasterxml.jackson.databind.*; import java.io.*; public class JsonIgnoreTypeTest {    public static void main(String args[]) throws IOException {       Employee emp = new Employee();       ObjectMapper mapper = new ObjectMapper();       String jsonString = mapper.writerWithDefaultPrettyPrinter().writeValueAsString(emp);       System.out.println(jsonString);    } } // Employee class class Employee {    @JsonIgnoreType    public static class Address {   ...

Read More

How to add a JSON string to an existing JSON file in Java?

raja
raja
Updated on 06-Jul-2020 7K+ Views

A Gson is a json library for Java and it can be used to generate a JSON. In the initial step, we can read a JSON file and parsing to a Java object then need to typecast the Java object to a JSonObject and parsing to a JsonArray. Then iterating this JSON array to print the JsonElement. We can create a JsonWriter class to write a JSON encoded value to a stream, one token at a time. Finally, a new JSON string can be written to an existing json file. Exampleimport java.io.*; import java.util.*; import com.google.gson.*; import com.google.gson.stream.*; import com.google.gson.annotations.*; public class JSONFilewriteTest {   ...

Read More
Showing 111–120 of 152 articles
« Prev 1 10 11 12 13 14 16 Next »
Advertisements