Found 206 Articles for JSON

How to convert JsonNode to ArrayNode using Jackson API in Java?

raja
Updated on 09-Jul-2020 07:59:35

11K+ Views

A JsonNode is a base class for all JSON nodes that forms the JSON Tree Model whereas ArrayNode is a node class that represents an array mapped from JSON content. We can convert or translate JsonNode to ArrayNode by typecasting the ArrayNode to retrieve the values using the readTree() method of ObjectMapper class and get() method for accessing the value of a specified element of an array node.Syntaxpublic JsonNode readTree(String content) throws IOException, com.fasterxml.jackson.core.JsonProcessingExceptionExampleimport com.fasterxml.jackson.databind.ObjectMapper; import com.fasterxml.jackson.databind.JsonNode; import com.fasterxml.jackson.databind.node.ArrayNode; import com.fasterxml.jackson.core.JsonProcessingException; public class JSonNodeToArrayNodeTest {    public static void main(String args[]) throws JsonProcessingException {       String jsonStr = "{\"Technologies\" : [\"Java\", ... Read More

How to search a value inside a JSON file using Jackson in Java?

raja
Updated on 09-Jul-2020 07:52:12

2K+ Views

The com.fasterxml.jackson.databind.node.ObjectNode class can be used to map the JSON object structure in Json content. We can search for a particular value inside the JSON file using the get() method of ObjectNode class, this method used for accessing the value of a specified field of an object node.Syntaxpublic JsonNode get(String fieldName)Exampleimport com.fasterxml.jackson.databind.ObjectMapper; import com.fasterxml.jackson.databind.node.ObjectNode; public class ObjectNodeTest {    public static void main(String args[]) throws Exception {       String jsonString = "{\"Id\":101, \"name\":\"Raja Ramesh\", \"address\":\"Madhapur\"}";       ObjectMapper mapper = new ObjectMapper();       ObjectNode node = mapper.readValue(jsonString, ObjectNode.class);       if(node.has("name")) {       ... Read More

When to use @ConstructorProperties annotation with Jackson in Java?

raja
Updated on 09-Jul-2020 07:19:56

1K+ Views

The @ConstructorProperties annotation is from java.beans package, used to deserialize JSON to java object via the annotated constructor. This annotation supports from Jackson 2.7 version onwards. The way this annotation works very simple, rather than annotating each parameter in the constructor, we can provide an array with the properties names for each of the constructor parameters.Syntax@Documented @Target(value=CONSTRUCTOR) @Retention(value=RUNTIME) public @interface ConstructorPropertiesExampleimport com.fasterxml.jackson.databind.ObjectMapper; import java.beans.ConstructorProperties; public class ConstructorPropertiesAnnotationTest {    public static void main(String args[]) throws Exception {       ObjectMapper mapper = new ObjectMapper();       Employee emp = new Employee(115, "Raja");       String jsonString = mapper.writerWithDefaultPrettyPrinter().writeValueAsString(emp);   ... Read More

How to add/insert additional property to JSON string using Gson in Java?

raja
Updated on 19-Feb-2020 10:28:54

7K+ Views

The com.google.gson.JSonElement class represents an element of Json. We can use the toJsonTree() method of Gson class to serialize an object's representation as a tree of JsonElements. We can add/ insert an additional property to JSON string by using the getAsJsonObject() method of JSonElement. This method returns to get the element as JsonObject.Syntaxpublic JsonObject getAsJsonObject()Exampleimport com.google.gson.*; public class AddPropertyGsonTest {    public static void main(String[] args) {       Gson gson = new GsonBuilder().setPrettyPrinting().create(); // pretty print JSON       Student student = new Student("Adithya");       String jsonStr = gson.toJson(student, Student.class);       System.out.println("JSON String: " + jsonStr);       ... Read More

Importance of @JsonUnwrapped annotation using Jackson in Java?

raja
Updated on 09-Jul-2020 06:43:52

375 Views

The @JsonUnwrapped annotation can be used to unwrap values during the serialization and deserialization process. It helps to render the values of a composed class as if it belongs to the parent class.Syntax@Target(value={ANNOTATION_TYPE, FIELD, METHOD, PARAMETER}) @Retention(value=RUNTIME) public @interface JsonUnwrappedExampleimport com.fasterxml.jackson.annotation.JsonUnwrapped; import com.fasterxml.jackson.databind.ObjectMapper; import com.fasterxml.jackson.core.JsonProcessingException; public class JsonUnwrappedAnnotationTest {    public static void main(String args[]) throws JsonProcessingException {       ObjectMapper mapper = new ObjectMapper();       String jsonString = mapper.writerWithDefaultPrettyPrinter().writeValueAsString(new Employee());       System.out.println(jsonString);    } } class Employee {    public int empId = 110;    public String empName = "Raja Ramesh";    @JsonUnwrapped   ... Read More

When to use @JsonValue annotation using Jackson in Java?

raja
Updated on 19-Feb-2020 10:02:51

4K+ Views

The @JsonValue annotation is useful at the method level. We can use this annotation to generate a JSON string from java object. If we want to print a serialized object then override the toString() method. But using @JsonValue annotation, we can define a way in which java object is serialized.Syntax@Target(value={ANNOTATION_TYPE, METHOD, FIELD}) @Retention(value=RUNTIME) public @interface JsonValueExampleimport com.fasterxml.jackson.annotation.JsonProperty; import com.fasterxml.jackson.annotation.JsonValue; import com.fasterxml.jackson.core.JsonProcessingException; import com.fasterxml.jackson.databind.ObjectMapper; import java.io.IOException; public class JsonValueAnnotationTest {    public static void main(String args[]) throws Exception {       ObjectMapper mapper = new ObjectMapper();       String jsonString = mapper.writeValueAsString(new Student());       System.out.println(jsonString);    } } ... Read More

How to control serialization through @JSON annotation using flexjson in Java?

raja
Updated on 09-Jul-2020 06:17:06

178 Views

The @JSON annotation is used by JSONSerializer class to exclude or include a field during the serialization process. We can use the serialize() method of JSONSerializer class to perform a shallow serialization of the target instance.Syntax@Retention(value=RUNTIME) @Target(value={FIELD, TYPE, METHOD}) public @interface JSONExampleimport flexjson.JSONSerializer; import flexjson.JSON; public class JSONAnnotationTest {    public static void main(String[] args) {       JSONSerializer serializer = new JSONSerializer().prettyPrint(true);       Employee emp = new Employee("Raja", "Ramesh", 30, "Hyderabad");       String jsonStr = serializer.serialize(emp);       System.out.println(jsonStr);    } } // Employee class class Employee {    private String firstName, lastName, address;    private ... Read More

How to implement custom deserializer using @JsonDeserialize annotation in Java?

raja
Updated on 09-Jul-2020 06:19:40

2K+ Views

The @JsonDeserialize annotation is used to declare custom deserializer while deserializing JSON to Java object. We can implement a  custom deserializer by extending the StdDeserializer class with a generic type Employee and need to override the deserialize() method of StdDeserializer class.Syntax@Target(value={ANNOTATION_TYPE, METHOD, FIELD, TYPE, PARAMETER}) @Retention(value=RUNTIME) public @interface JsonDeserializeIn the below program, we can implement a custom deserializer using @JsonDeserialize annotationExampleimport java.io.*; import com.fasterxml.jackson.core.*; import com.fasterxml.jackson.databind.*; import com.fasterxml.jackson.databind.annotation.*; import com.fasterxml.jackson.databind.deser.std.*; public class JsonDeSerializeAnnotationTest {    public static void main (String[] args) throws JsonProcessingException, IOException {       Employee emp = new Employee(115, "Adithya");       ObjectMapper mapper = new ObjectMapper();     ... Read More

How to implement custom serializer using @JsonSerialize annotation in Java?

raja
Updated on 09-Jul-2020 05:49:14

2K+ Views

The @JsonSerialize annotation is used to declare custom serializer during the serialization of a field. We can implement a custom serializer by extending the StdSeralizer class. and need to override the serialize() method of StdSerializer class.Syntax@Target(value={ANNOTATION_TYPE, METHOD, FIELD, TYPE, PARAMETER}) @Retention(value=RUNTIME) public @interface JsonSerializeIn the below program, we can implement a custom serializer using @JsonSerialize annotationExampleimport java.io.*; import com.fasterxml.jackson.core.*; import com.fasterxml.jackson.databind.*; import com.fasterxml.jackson.databind.annotation.*; import com.fasterxml.jackson.databind.ser.std.*; public class JsonSerializeAnnotationTest {    public static void main (String[] args) throws JsonProcessingException, IOException {       Employee emp = new Employee(115, "Adithya", new String[] {"Java", "Python", "Scala"});       ObjectMapper mapper = new ... Read More

How to convert a JSON string to a bean using JSON-lib API in Java?

raja
Updated on 09-Jul-2020 05:18:38

977 Views

The JSON-lib API is a java library to serialize and de-serialize java beans, maps, arrays, and collections in the JSON format. We need to convert a JSON string to a bean by converting a string to JSON object first then convert this to a java bean.Syntaxpublic static Object toBean(JSONObject jsonObject, Class beanClass)In the below program, we can convert a JSON string to a bean.Exampleimport net.sf.json.JSONObject; import net.sf.json.JSONSerializer; public class ConvertJSONStringToBeanTest {    public static void main(String[] args) {       String jsonStr = "{\"firstName\": \"Adithya\", \"lastName\": \"Sai\", \"age\": 30, \"technology\": \"Java\"}";       JSONObject jsonObj = (JSONObject)JSONSerializer.toJSON(jsonStr); // convert String ... Read More

Previous 1 ... 4 5 6 7 8 ... 21 Next
Advertisements