Found 2616 Articles for Java

How to access the JSON fields, arrays and nested objects of JsonNode in Java?

raja
Updated on 25-Oct-2023 14:06:16

27K+ Views

A JsonNode is Jackson's tree model for JSON and it can read JSON into a JsonNode instance and write a JsonNode out to JSON. To read JSON into a JsonNode with Jackson by creating ObjectMapper instance and call the readValue() method. We can access a field, array or nested object using the get() method of JsonNode class. We can return a valid string representation using the asText() method and convert the value of the node to a Java int using the asInt() method of JsonNode class.In the below example, we can access JSON fields, arrays and nested objects of JsonNode.Exampleimport com.fasterxml.jackson.databind.*; import java.io.*; public class ... Read More

How to implement custom JSON de-serialization with Gson in Java?

raja
Updated on 13-Feb-2020 10:11:34

522 Views

A Gson library provides a way to specify custom de-serializers by registering a custom de-serializer with the GsonBuilder if we need a way to convert a java object to JSON. We can create a custom de-serializer by overriding the deserialize() method of com.google.gson.JsonDeserializer class.In the below example, the implementation of custom de-serialization of JSON.Exampleimport java.lang.reflect.Type; import com.google.gson.*; public class CustomJSONDeSerializerTest {    public static void main(String[] args) {       Gson gson = new GsonBuilder().registerTypeAdapter(Password.class, new          PasswordDeserializer()).setPrettyPrinting().create();       String jsonStr = "{" +                           "\"firstName\": ... Read More

Importance of a JSONTokener in Java?

raja
Updated on 13-Feb-2020 10:12:30

2K+ Views

The JSONTokener class allows an application to break a string into tokens. It can be used by the JSONObject and JSONArray constructors to parse JSON source strings. A few important methods of JSONTokener class are back() - moves cursor one position back, more() - returns true if the token has element or else returns false,  next() - returns a character next to current position and nextTo(character) - returns a string until the given character matches.Syntaxpublic class JSONTokener extends java.lang.ObjectExampleimport java.io.*; import org.json.*; public class JSONTokenerTest {    public static void main(String args[]) throws JSONException, Exception {       String jsonStr = "{" + " \"Technology\": ... Read More

Java program to count the occurrences of each character

AmitDiwan
Updated on 04-Jul-2024 17:35:45

2K+ Views

In Java, counting the occurrences of each character in a string is a common task that can be efficiently performed using a HashMap. A HashMap allows us to store key-value pairs, where each unique character in the string is a key, and the value is the count of its occurrences. Problem Statement Given a string, we need to count how many times each character appears in the string. For example, in the string thisisit, the character t appears twice, h appears once, i appears three times, and s appears twice. Input String myStr = "thisisit"; Output Counting ... Read More

Java program to check palindrome

AmitDiwan
Updated on 10-Aug-2023 12:50:14

3K+ Views

The term palindrome is derived from the Greek word 'palin dromo' which means running back again. A number, string or phrase that remains the same when reversed i.e. reads the same backward as forwards is called a palindrome. For instance, numerical palindromes include numbers like 525, 2002 and 12121. And, word palindromes are 'ABA', 'BABAB' and 'RADAR'. To check palindrome in Java, we can use the while loop and if-else block that will identify whether the given numbers or strings and their reverse are the same or not. Java Program to Check Plaindrome In this section, we will write ... Read More

Convert List of Characters to String in Java

AmitDiwan
Updated on 04-Dec-2023 11:26:04

343 Views

Let’s say the following is our list of characters − List list = Arrays.asList('W', 'e', 'l', 'c', 'o', 'm', 'e'); Convert the list of characters to string − String string = list.stream().map(String::valueOf).collect(Collectors.joining()); Example Following is the program to convert List of Characters to String in Java − import java.util.stream.Collectors; import java.util.Arrays; import java.util.List; public class Demo { public static void main(String[] args) { List list = Arrays.asList('W', 'e', 'l', 'c', 'o', 'm', 'e'); String string = list.stream().map(String::valueOf).collect(Collectors.joining()); System.out.println("String = "+string); } } Output String = Welcome Read More

How can we add a JSONArray within JSONObject in Java?

raja
Updated on 04-Jul-2020 08:50:58

4K+ Views

A JSONObject can parse text from a String to produce a map-like object and a JSONArray can parse text from a String to produce a vector-like object. We can also add a JSONArray within JSONObject by first creating a JSONArray with few items and add these array of items to the put() method of JSONObject class.Syntaxpublic JSONObject put(java.lang.String key, java.util.Collection value) throws JSONExceptionExampleimport org.json.*; public class AddJSONArrayTest {    public static void main(String[] args) throws JSONException {       JSONArray array = new JSONArray();       array.put("INDIA");       array.put("AUSTRALIA");       array.put("ENGLAND");       JSONObject obj = ... Read More

How to implement custom JSON serialization with Gson in Java?

raja
Updated on 04-Jul-2020 08:51:40

998 Views

A Gson library provides a way to specify custom serializers by registering a custom serializer with the GsonBuilder if we need a way to convert a java object to JSON. We can create a custom serializer by overriding the serialize() method of com.google.gson.JsonSerializer class.In the below example, the implementation of custom serialization of JSON.Exampleimport java.lang.reflect.Type; import com.google.gson.*; public class CustomJSONSerializerTest {    public static void main(String[] args) {       Gson gson = new GsonBuilder().registerTypeAdapter(Password.class, new PasswordSerializer()) .setPrettyPrinting().create();       Student student = new Student("Adithya", "Jai", 25, "Chennai");       student.setPassword(new Password("admin@123"));       System.out.println(gson.toJson(student));    } } class PasswordSerializer ... Read More

How to convert XML to JSON array in Java?

raja
Updated on 04-Jul-2020 08:43:57

6K+ Views

A JSON is a lightweight data-interchange format and the format of JSON is like a key-value pair. We can convert XML to JSON array using org.json.XML class, this provides a static method, XML.toJSONObject() to convert XML to JSON array.Syntaxpublic static JSONObject toJSONObject(java.lang.String string) throws JSONExceptionIn the below example, converting XML to JSON arrayExampleimport org.json.*; public class ConvertXMLToJSONArrayTest {    public static String xmlString= "tutorialspointtutorix";    public static void main(String[] args) {       try {          JSONObject json = XML.toJSONObject(xmlString); // converts xml to json          String jsonPrettyPrintString = json.toString(4); // json pretty print     ... Read More

How to write a JSON string to file using the Gson library in Java?

raja
Updated on 04-Jul-2020 08:37:01

1K+ Views

A Gson is a library that can be used to convert Java Objects to JSON representation. The primary class to use is Gson which we can create by calling the new Gson() and the GsonBuilder class can be used to create a Gson instance.We can write a JSON string to file using the toJson() method of Gson class in the below exampleExampleimport java.io.*; import com.google.gson.*; public class JSONToFileTest {    public static void main(String[] args) throws IOException {       Gson gson = new Gson();       FileWriter fileWriter = new FileWriter("Student.json");       Student student = new Student("Raja", "Ramesh", ... Read More

Advertisements