Found 7442 Articles for Java

How do we split a string with any whitespace chars as delimiters using java?

Maruthi Krishna
Updated on 01-Sep-2025 12:30:57

845 Views

What is split() method in Java? The split() method of the String class accepts a delimiter (in the form of a string), divides the current String into smaller strings based on the delimiter, and returns the resulting strings as an array. If the String does not contain the specified delimiter, this method returns an array that contains only the current string. If the String does not contain the specified delimiter, this method returns an array containing the whole string as an element. Splitting the string with white space as delimiter Following are steps to split a String into an array ... Read More

How to convert Java object to JSON using Jackson library?

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

51K+ 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

2K+ 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

330 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

8K+ 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

4K+ 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

2K+ 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 POJO to XML using the Jackson library in Java?

Aishwarya Naglot
Updated on 01-Sep-2025 13:35:36

7K+ Views

Jackson is a library that allows you to convert Java objects into XML and vice versa. In this example, we will demonstrate how to convert a POJO (Plain Old Java Object) into XML using the Jackson library. Well, if you are not familiar with POJO, it is a simple Java object that does not follow any specific framework or design pattern. It is just a regular Java class with fields and methods. We will use the method writeValueAsString() of the XmlMapper class to convert a POJO into XML. The XmlMapper class is part of the Jackson library and it ... Read More

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

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

3K+ 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?

Aishwarya Naglot
Updated on 13-May-2025 15:46:49

1K+ Views

Parsing a JSON without duplicate keys means converting a JSON string into a Java object. But we need to keep in mind that the JSON string should not have duplicate keys. Gson: Parsing a JSON Without Duplicate Keys We can use the Gson library to parse a JSON without duplicate keys in Java. It is developed by Google and used for converting Java objects into JSON and vice versa. By default, Gson does not allow duplicate keys in JSON. If a JSON string has duplicate keys, Gson will throw a JsonSyntaxException. We will use the methods fromJson() and toJson() to achieve ... Read More

Advertisements