Found 206 Articles for JSON

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

915 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

How to get all the keys of a JSON object using GSON in Java?

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

3K+ Views

A Gson is a library that can be used to parse Java objects to JSON and vice-versa. It can also be used to convert a JSON string to an equivalent Java object. In order to parse java object to JSON or JSON to java object, we need to import com.google.gson package in the Java program.We can get all the keys of a JSON object in the below exampleExampleimport java.util.*; import com.google.gson.*; import org.json.*; public class GetJSONAllKeysTest {    public static void main(String[] args) {       String jsonStr = "{\"Raja\":\"Java\", \"Ravi\":\"SAP\", \"Chaitanya\":\"Python\", \"Adithya\":\"Spark\"}";       JsonParser parser = new JsonParser();   ... Read More

How to serialize and de-serialize generic types using the Gson library in Java?

raja
Updated on 04-Jul-2020 08:08:23

996 Views

If a Java class is a generic type and we are using it with the Gson library for JSON serialization and deserialization. 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 java.lang.ObjectExampleimport java.lang.reflect.Type; import java.util.*; import com.google.gson.*; import com.google.gson.reflect.*; public class GenericTypesJSONTest {    public static void main(String[] args) {       Gson gson = new GsonBuilder().setPrettyPrinting().create();       List list = Arrays.asList("INDIA", "AUSTRALIA", "ENGLAND", "SOUTH AFRICA");   ... Read More

Pretty print JSON using Jackson library in Java?

raja
Updated on 04-Jul-2020 08:02:26

2K+ Views

A Jackson API 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 can process a JSON in three different ways using Streaming API, Tree Model, and Data Binding.We can Pretty print JSON using the writerWithDefaultPrettyPrinter() of ObjectMapper class, it is a factory method for constructing ObjectWriter that will serialize objects using the default pretty printer for indentation.Syntaxpublic ObjectWriter writerWithDefaultPrettyPrinter()Exampleimport java.io.IOException; import com.fasterxml.jackson.databind.ObjectMapper; public class PrettyPrintJsonJacksonTest {    public static void main(String[] args) ... Read More

Convert JSON to/from Map using Jackson library in Java?

raja
Updated on 04-Jul-2020 07:58:38

1K+ Views

The JSON Jackson is a library for Java and 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 convert JSON to/from Map using readValue() and writeValueAsString() methods of com.fasterxml.jackson.databind.ObjectMapper class.JSON to MapSyntaxpublic T readValue(String content, TypeReference valueTypeRef) throws IOException, JsonParseException, JsonMappingExceptionExampleimport java.io.*; import java.util.*; import com.fasterxml.jackson.databind.ObjectMapper; import com.fasterxml.jackson.core.type.TypeReference; public class ConvertJSONToMapTest {    public static void main(String args[]) {       try {         ObjectMapper mapper = new ObjectMapper();          String jsonString = "{\"Name\":\"Raja\", \"Technology\":\"Java\"}";     ... Read More

How to convert a JSON array to CSV in Java?

raja
Updated on 04-Jul-2020 07:49:16

4K+ Views

The JSON can be used as a data-interchange format and is 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 JSON Array to CSV format using org.json.CDL class, it can provide a static method toString(),  to convert a JSONArray into comma-delimited text. We need to import org.apache.commons.io.FileUtils package to store the data in a CSV file using the writeStringToFile() method.Syntaxpublic static java.lang.String toString(JSONArray ja) throws JSONExceptionIn the below example, we can convert a JSON Array to CSV format.Exampleimport java.io.File; import org.apache.commons.io.FileUtils; import org.json.*; public class ConvertJsonToCSVTest {    public static void main(String[] ... Read More

How to pretty print JSON using the Gson library in Java?

raja
Updated on 04-Jul-2020 07:29:36

3K+ 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. By default, Gson can print the JSON in compact format. To enable Gson pretty print, we must configure the Gson instance using the setPrettyPrinting() method of GsonBuilder class and this method configures Gson to output JSON that fits in a page for pretty printing.Syntaxpublic GsonBuilder setPrettyPrinting()Exampleimport java.util.*; import com.google.gson.*; public class PrettyJSONTest {    public static void main( String[] args ) {       Employee emp = new Employee("Raja", "115", "Content Engineer", "Java", "Hyderabad");     ... Read More

Advertisements