Found 206 Articles for JSON

Convert a Map to JSON using the Gson library in Java?

raja
Updated on 04-Jul-2020 07:19:48

2K+ 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 our Java program.We can create a Gson instance in two waysBy using new Gson().By creating a GsonBuilder instance and calling with the create() method.In the below program, we can convert a Map to a JSON object.Exampleimport java.lang.reflect.*; import java.util.*; import com.google.gson.*; import com.google.gson.reflect.*; public class ConverMapToJsonTest {    public static ... Read More

Convert a list of objects to JSON using the Gson library in Java?

raja
Updated on 04-Jul-2020 07:14:39

6K+ Views

A Gson is a library that can be used to convert Java Objects to JSON representation. It can also be used to convert a JSON string to an equivalent Java object. 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 convert a list of objects by first creating a Person class and convert a list of Person objects to JSON.Exampleimport java.util.*; import java.util.stream.*; import com.google.gson.*; public class JSONConverterTest {    public static void main( String[] args ) {       Gson gson = new ... Read More

Convert JSON object to Java object using Gson library in Java?

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

8K+ Views

A Gson is a json library for java, which is created by Google and it can be used to generate a JSON. By using Gson, we can generate JSON and convert JSON to java objects. We can call the fromJson() method of Gson class to convert a JSON object to Java Object.Syntaxpublic fromJson(java.lang.String json, java.lang.Class classOfT) throws JsonSyntaxExceptionExampleimport com.google.gson.*; public class JSONtoJavaObjTest {    public static void main(String[] args) {       Gson gson = new Gson();       Emp emp = gson.fromJson("{'name':'raja', 'age':25}", Emp.class);       System.out.println(emp.getName());       System.out.println(emp.getAge());    } } // Emp class class Emp ... Read More

How can we check if a JSON object is empty or not in Java?

raja
Updated on 04-Jul-2020 06:54:20

7K+ Views

A JSON is a lightweight data-interchange format and the format of JSON is a key with value pair. The JSONObject can parse a text from a String to produce a map-like object and supports java.util.Map interface. We can check whether the JSON object is empty or not in the below exampleExampleimport java.util.*; import org.json.*; public class JSONObjectTest {    public static void main(String[] args) {       JSONObject jsonObj = new JSONObject(          "{" +             "Name : Jai," +             "Age : 25, " +             "Salary: 25000.00 " +          "}"       );       if(jsonObj.isEmpty()) {          System.out.println("JSON is empty");       } else {          System.out.println("JSON is not empty");       }    } }OutputJSON is not empty

How to get the values of the different types from a JSON object in Java?

raja
Updated on 14-Sep-2023 21:54:22

26K+ Views

A JSONObject is an unordered collection of name/value pairs and parses text from a String to produce a map-like object. A JSONObject has few important methods to display the values of different types like getString() method to get the string associated with a key string, getInt() method to get the int value associated with a key,  getDouble() method to get the double value associated with a key and getBoolean() method to get the Boolean value associated with a key.Exampleimport org.json.*; public class JSONObjectTypeValuesTest {    public static void main(String[] args) throws JSONException {       JSONObject jsonObj = new JSONObject(          "{" ... Read More

When can we use a JSONStringer in Java?

raja
Updated on 04-Jul-2020 06:49:30

370 Views

A JSONStringer provides a convenient way of producing JSON text and it can strictly follow to JSON syntax rules. Each instance of JSONStringer can produce one JSON text. A JSONStringer instance provides a value-method for appending values to the text and a key-method for adding keys before values in objects. There is an array () and endArray() methods that make and bound array values and object() and end object() methods that make and bound object values.Example 1import org.json.*; public class JSONStringerTest1 {    public static void main(String[] args) throws JSONException {       JSONStringer stringer = new JSONStringer();       String jsonStr = stringer ... Read More

How can we sort a JSONArray in Java?

raja
Updated on 04-Jul-2020 06:50:23

6K+ Views

The JSON is one of the widely used data-interchange formats and 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 sort a JSONArray in the below example.Exampleimport java.util.*; import org.json.*; public class SortJSONArrayTest {    public static void main(String[] args) {       String jsonStr = "[ { \"ID\": \"115\", \"Name\": \"Raja\" }, { \"ID\": \"120\", \"Name\": \"Jai\" }, { \"ID\": \"125\", \"Name\": \"Adithya\" }]";       JSONArray jsonArray = new JSONArray(jsonStr);       JSONArray sortedJsonArray = new JSONArray();       List list = new ArrayList(); ... Read More

How can we convert a list to the JSON array in Java?

raja
Updated on 12-Sep-2023 02:54:07

29K+ Views

The JSON is a lightweight, text-based and language-independent data exchange format. The JSON can represent two structured types like objects and arrays. An object is an unordered collection of key/value pairs and an array is an ordered sequence of values.We can convert a list to the JSON array using the JSONArray.toJSONString() method and it is a static method of JSONArray, it will convert a list to JSON text and the result is a JSON array.Syntaxpublic static java.lang.String toJSONString(java.util.List list)Exampleimport java.util.*; import org.json.simple.*; public class ConvertListToJSONArrayTest {    public static void main(String[] args) {       List list = new ArrayList();       list.add("India");       ... Read More

How can we decode a JSON object in Java?

raja
Updated on 04-Jul-2020 06:27:24

2K+ Views

A JSON is a lightweight, text-based and language-independent data exchange format. A JSON can represent two structured types like objects and arrays. We can decode a JSON object using JSONObject and JSONArray from json.simple API. A JSONObject works as a java.util.Map whereas JSONArray works as a java.util.List.In the below example, we can decode a JSON object.Exampleimport org.json.simple.*; import org.json.simple.parser.*; public class JSONDecodingTest {    public static void main(String[] args) {       JSONParser parser = new JSONParser();       String str = "[ 0 , {\"1\" : { \"2\" : {\"3\" : {\"4\" : [5, { \"6\" : { \"7\" : 8 } } ] } ... Read More

How can we encode a JSON object in Java?

raja
Updated on 04-Jul-2020 05:58:28

2K+ Views

A JSONObject is a subclass of java.util.HashMap where no order is provided. We can also use the strict ordering of elements as well with the help of the JSONValue.toJSONString(map) method i.e. by the implementation of java.util.LinkedHashMap.We can encode a JSON object in the below two examples.Example import java.util.*; import org.json.simple.JSONObject; public class JSONEncodingTest {    public static void main(String[] args) {       Map dataMap = new HashMap();       dataMap.put("Name", "Adithya");       dataMap.put("Age", new Integer(25));       dataMap.put("Salary", new Double(25000.00));       dataMap.put("Employee Id", new Integer(115));       dataMap.put("Company", "TutorialsPoint");       JSONObject ... Read More

Advertisements