Found 206 Articles for JSON

How to rename the properties of JSON using Gson in Java?

raja
Updated on 06-Jul-2020 13:17:10

3K+ Views

The Gson @SerializedName annotation can be serialized to a JSON with the provided name value as its field name. This annotation can override any FieldNamingPolicy including the default field naming policy that may have been set on the Gson instance. A different naming policy can set using the GsonBuilder class.Syntax@Retention(value=RUNTIME) @Target(value={FIELD, METHOD}) public @interface SerializedNameExampleimport com.google.gson.annotations.*; import com.google.gson.*; public class SerializedNameAnnotationTest {    public static void main(String args[]) {       Employee emp = new Employee("Rahul", "Dev", 30, "Nagpur");       Gson gson = new GsonBuilder().setPrettyPrinting().create(); // pretty print       String jsonStr = gson.toJson(emp);       System.out.println(jsonStr);    } ... Read More

How to ignore the multiple properties of a JSON object in Java?

raja
Updated on 06-Jul-2020 13:18:16

3K+ Views

The @JsonIgnoreProperties Jackson annotation can be used to specify a list of properties or fields of a class to ignore. The @JsonIgnoreProperties annotation can be placed above the class declaration instead of above the individual properties or fields to ignore.Syntax@Target(value={ANNOTATION_TYPE, TYPE, METHOD, CONSTRUCTOR, FIELD}) @Retention(value=RUNTIME) public @interface JsonIgnorePropertiesExampleimport java.io.*; import com.fasterxml.jackson.annotation.*; import com.fasterxml.jackson.databind.*; public class JsonIgnorePropertiesTest {    public static void main(String[] args) throws IOException {       Customer customer = new Customer("120", "Ravi", "Hyderabad");       System.out.println(customer);       ObjectMapper mapper = new ObjectMapper();       String jsonString = mapper.writeValueAsString(customer);       System.out.println("JSON: " + jsonString);     ... Read More

How to serialize a JSON string to an Output Handler in Java?

raja
Updated on 06-Jul-2020 13:11:16

509 Views

The Flexjson is a lightweight library for serializing and deserializing Java objects into and from JSON format. A JSONSerializer is the main class for performing serialization of Java objects to JSON. We can serialize a JSON string to an Output Handler using the WriterOutputHandler class and it implements the OutputHandler interface.Syntaxpublic class WriterOutputHandler extends Object implements OutputHandlerExampleimport java.io.*; import flexjson.JSONSerializer; import flexjson.OutputHandler; import flexjson.WriterOutputHandler; public class JsonOutputHandlerTest {    public static void main(String[] args) {       JSONSerializer serializer = new JSONSerializer().prettyPrint(true); // pretty print JSON       Employee emp = new Employee("Raja", "Ramesh", 28, "Hyderabad");       OutputHandler out = new WriterOutputHandler(new ... Read More

How to ignore a field of JSON object using the Jackson library in Java?

raja
Updated on 06-Jul-2020 13:12:32

7K+ Views

The Jackson @JsonIgnore annotation can be used to ignore a certain property or field of a Java object. The property can be ignored both when reading JSON into Java objects and when writing Java objects into JSON. We can use the readValue() and writeValueAsString() methods of ObjectMapper class to read a JSON to Java Object and to write a Java object to JSON.Syntax@Target(value={ANNOTATION_TYPE, METHOD, CONSTRUCTOR, FIELD}) @Retention(value=RUNTIME) public @interface JsonIgnoreExampleimport java.io.*; import com.fasterxml.jackson.annotation.*; import com.fasterxml.jackson.databind.*; public class JsonIgnoreTest {    public static void main(String[] args) throws IOException {       Customer customer = new Customer("110", "Surya Kiran", "Chennai");       System.out.println(customer); ... Read More

How to deserialize a JSON string using @JsonCreator annotation in Java?

raja
Updated on 17-Feb-2020 08:18:20

791 Views

The @JsonProperty annotation can be used to indicate the property name in JSON. This annotation can be used for a constructor or factory method. The @JsonCreator annotation is useful in situations where the @JsonSetter annotation cannot be used. For instance, immutable objects do not have any setter methods, so they need their initial values injected into the constructor.@JsonProperty - ConstructorExampleimport com.fasterxml.jackson.annotation.*; import java.io.IOException; import com.fasterxml.jackson.databind.*; public class JsonCreatorTest1 {    public static void main(String[] args) throws IOException {       ObjectMapper om = new ObjectMapper();       String jsonString = "{\"id\":\"101\", \"fullname\":\"Ravi Chandra\", \"location\":\"Pune\"}";       System.out.println("JSON: " + jsonString);   ... Read More

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?

raja
Updated on 07-Sep-2023 00:47:49

37K+ Views

The ObjectMapper class is the most important class in the Jackson library. We can convert a JSON to Java Object using the readValue() method of ObjectMapper class, this method deserializes a JSON content from given JSON content String.Syntaxpublic readValue(String content, JavaType valueType) throws IOException, JsonParseException, JsonMappingExceptionExampleimport java.io.*; import java.util.*; import com.fasterxml.jackson.core.*; import com.fasterxml.jackson.databind.*; public class JSONToJavaObjectTest {    public static void main(String args[]) throws JsonGenerationException, JsonMappingException, IOException {       Employee emp1 = new Employee();       emp1.setFirstName("Raja");       emp1.setLastName("Ramesh");       emp1.setId(115);       emp1.getTechnologies().add("Java");       emp1.getTechnologies().add("Selenium");       emp1.getTechnologies().add("Spark"); ... Read More

How to wrap a JSON using flexjson in Java?

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

882 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?

raja
Updated on 06-Jul-2020 12:42:31

3K+ Views

The JSON is one of the widely used data-interchange formats. It is a lightweight and language independent. A JSONArray can parse text from a String to produce a vector-like object and supports java.util.List interface.We can convert a JSONArray to String Array in the below exampleExampleimport org.json.*; import java.util.*; public class JsonArraytoStringArrayTest {    public static void main(String[] args) {       JSONArray jsonArray = new JSONArray();       jsonArray.put("INDIA ");       jsonArray.put("AUSTRALIA ");       jsonArray.put("SOUTH AFRICA ");       jsonArray.put("ENGLAND ");       jsonArray.put("NEWZEALAND ");       List list = new ArrayList();       for(int i=0; ... Read More

Advertisements