Found 2616 Articles for Java

How to convert Java object to JSON using Jackson library?

Maruthi Krishna
Updated on 06-Sep-2023 11:49:04

46K+ Views

JSON or JavaScript Object Notation is a lightweight text-based open standard designed for human-readable data interchange. Conventions used by JSON are known to programmers, which include C, C++, Java, Python, Perl, etc.There are several Java libraries available to handle JSON objects. Jackson is a simple java based library to serialize java objects to JSON and vice versa.Converting Java object to JSONThe ObjectMapper class of the Jackson API in Java provides methods to convert a Java object to JSON object and vice versa.The writeValueAsString() method of this class accepts a JSON object as a parameter and returns its respective JSON StringTherefore, ... Read More

How to convert Java object to JSON using GSON library?

Maruthi Krishna
Updated on 10-Oct-2019 06:35:44

1K+ Views

JSON or JavaScript Object Notation is a lightweight text-based open standard designed for human-readable data interchange. Conventions used by JSON are known to programmers, which include C, C++, Java, Python, Perl, etc.There are several Java libraries available to handle JSON objects. Google Gson is a simple Java-based library to serialize Java objects to JSON and vice versa. It is an open-source library developed by Google.Converting Java object to JSONThe Google's Gson library provides a class with the same name (Gson) which is the main class of the library.This class provides a method named toJson() there are several variants of this ... Read More

In how many ways we can convert a String to a character array using Java?

Maruthi Krishna
Updated on 10-Oct-2019 06:32:04

200 Views

You can convert a String to a character array either by copying each element of the String to an array or, using the toCharArray() method.Copying each elementGet the String to be converted.Create an empty character array with the length of the String.The charAt() method of the String class returns the character at a particular position. Using this method copy each character of the String to the array.Example Live Demoimport java.util.Arrays; import java.util.Scanner; public class StringToCharArray {    public static void main(String args[]) {       Scanner sc = new Scanner(System.in);       System.out.println("Enter a String value: ");     ... Read More

Convert CSV to JSON using the Jackson library in Java?

raja
Updated on 06-Jul-2020 11:44:03

7K+ Views

A Jackson is a Java JSON API that provides several different ways to work with JSON. We can convert CSV data to JSON data using the CsvMapper class, it is specialized ObjectMapper, with extended functionality to produce CsvSchema instances out of POJOs. We can use the reader() method for constructing ObjectReader with default settings. In order to convert this, we need to import the com.fasterxml.jackson.dataformat.csv package.In the below example, convert a CSV to JSON.Exampleimport java.io.*; import java.util.*; import com.fasterxml.jackson.databind.*; import com.fasterxml.jackson.dataformat.csv.*; public class CsvToJsonTest {    public static void main(String args[]) throws Exception {       File input = new File("input.csv");       try { ... Read More

Update a MySQL table with Java MySQL

AmitDiwan
Updated on 14-Feb-2020 10:24:53

3K+ Views

For this, you need to use PreparedStatement in Java for update. Let us first create a table −mysql> create table DemoTable(    Id int,    FirstName varchar(40) ); Query OK, 0 rows affected (0.62 sec)Insert some records in the table using insert command −mysql> insert into DemoTable values(100, 'Chris'); Query OK, 1 row affected (0.13 sec) mysql> insert into DemoTable values(111, 'Mike'); Query OK, 1 row affected (0.18 sec) mysql> insert into DemoTable values(121, 'Sam'); Query OK, 1 row affected (0.09 sec)Display all records from the table using select statement −mysql> select * from DemoTable;This will produce the following output ... Read More

How to deserialize a JSON array to list generic type in Java?

raja
Updated on 06-Jul-2020 11:38:01

1K+ Views

The Gson library provides a class called com.google.gson.reflect.TypeToken to store generic types by creating a Gson TypeToken class and pass the class type. Using this type, Gson can able to know the class passed in the generic class.Syntaxpublic class TypeToken extends ObjectWe can deserialize a JSON array to a generic type of list in the below exampleExampleimport java.lang.reflect.Type; import java.util.*; import com.google.gson.*; import com.google.gson.reflect.*; public class JSONArrayToListTest {    public static void main(String args[]) throws Exception {       String jsonStr = "[{\"name\":\"Adithya\", \"course\":\"Java\"}, " + "{\"name\":\"Ravi\", \"course\":\"Python\"}]";       Type listType = new TypeToken() {}.getType();       List students = ... Read More

Convert XML to POJO using the Jackson library in Java?

raja
Updated on 14-Feb-2020 10:12:44

7K+ Views

The JSON Jackson is a library for Java. It has very powerful data binding capabilities and provides a framework to serialize custom java objects to JSON and deserialize JSON back to Java object. We can also convert an XML format to the POJO object using the readValue() method of the XmlMapper class.Syntaxpublic T readValue(XMLStreamReader r, Class valueType) throws IOExceptionExampleimport com.fasterxml.jackson.dataformat.xml.*; public class XMLToPOJOTest {    public static void main(String args[]) throws Exception {       try {          XmlMapper xmlMapper = new XmlMapper();          Person pojo = xmlMapper.readValue(getXmlString(), Person.class);          System.out.println(pojo); ... Read More

Convert POJO to XML using the Jackson library in Java?

raja
Updated on 06-Jul-2020 09:24:08

5K+ Views

A Jackson is a Java-based library and it can be useful to convert Java objects to JSON and JSON to Java Object. A Jackson API is faster than other API, needs less memory area and is good for the large objects. We convert a POJO to XML format using the writeValueAsString() method of XmlMapper class and we need to pass the corresponding POJO instance as an argument to this method.Syntaxpublic String writeValueAsString(Object value) throws JsonProcessingExceptionExampleimport com.fasterxml.jackson.dataformat.xml.*; public class POJOToXmlTest {    public static void main(String args[]) throws Exception {       try {          XmlMapper xmlMapper = new ... Read More

How to insert DATE into a MySQL column value using Java?

AmitDiwan
Updated on 14-Feb-2020 10:23:16

2K+ Views

For this, you can use PreparedStatement from Java. Let us first create a table wherein one of the columns is ArrivalDate with DATE type −mysql> create table DemoTable(    PassengerId int,    PassengerName varchar(40),    ArrivalDate date ); Query OK, 0 rows affected (0.82 sec)The JAVA code is as follows to insert date −import java.sql.Connection; import java.sql.DriverManager; import java.sql.PreparedStatement; public class InsertDateFromJava {    public static void main(String[] args) {       Connection con = null;       PreparedStatement ps = null;       try {          java.util.Date javaDate = new java.util.Date();     ... Read More

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

raja
Updated on 06-Jul-2020 08:51:36

916 Views

A Gson is a JSON library for Java, which is created by Google. By using Gson, we can generate JSON and convert JSON to Java objects. We can create a Gson instance by creating a GsonBuilder instance and calling with the create() method. We can parse a JSON without duplicate keys using the TypeToken class. If we want to create a type literal for Map, we can create an empty anonymous inner class. If we try to insert a duplicate key, it will generate an error at runtime, "Exception in thread "main" com.google.gson.JsonSyntaxException: duplicate key"Syntaxpublic class TypeToken extends ObjectExampleimport java.lang.reflect.Type; import java.util.Map; import ... Read More

Advertisements