Found 2616 Articles for Java

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

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

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

400 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

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

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

Difference Between LinkedList and LinkedHashSet in Java

Nitin Sharma
Updated on 07-Dec-2023 11:12:55

1K+ Views

LinkedList and LinkedHashSet both are one of the most important classes of Java Collection framework. Following are the important differences between LinkedList and LinkedHashSet. Sr. No. Key LinkedList LinkedHashSet 1 Implementation LinkedList is the implementation of list and deque interface. LinkedHashSet on other hand is the implementation of set interface and it inherits Hashset class. 2 Internal Implementation LinkedList internally implements or we can say uses doubly linked list to store the elements. LinkedHashSet on other hand internally uses LinkedHashMap to store it’s elements. 3 Order of Elements As LinkedList internally used doubly linked list so we can add or remove elements from both ends in case of linkedlist. While LinkedHashset has Hashmap internally so elements could ... 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

How can we parse a nested JSON object in Java?

raja
Updated on 04-Jul-2020 05:48:09

13K+ 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. A JSONArray can parse text from a String to produce a vector-like object. We can parse a nested JSON object using the getString(index) method of JSONArray. This is a convenience method for the getJSONString(index).getString() method and it returns a string value at the specified position.SyntaxString getString(int index)Exampleimport java.util.*; import org.json.*; public class NestedJSONObjectTest {    public static void main(String args[]) {       String jsonDataString = "{userInfo : [{username:abc123}, {username:xyz123}, {username:pqr123},   {username:mno123}, {username:jkl123}]}";       JSONObject jsonObject = new JSONObject(jsonDataString); ... Read More

Convert a JSON object to XML format in Java?

raja
Updated on 04-Jul-2020 05:48:56

6K+ Views

A JSON is a lightweight data-interchange format and the format of JSON is like a key-value pair. We can convert a JSONObject into an XML format using org.json.XML class, this provides static methods to convert an XML text into a JSONObject and to convert a JSONObject into an XML text. The XML.toString() method convert a JSON object into a well-formed, element-normal XML string.Syntaxpublic static java.lang.String toString(java.lang.Object object) throws JSONExceptionExampleimport java.io.*; import org.json.*; public class JSONtoXMLTest {    public static void main(String[] args) throws JSONException {       String json = "{employee : { age:30, name : Raja, technology:Java}}";       //Convert JSON to ... Read More

Convert a JSON String to Java Object using the json-simple library in Java?

raja
Updated on 04-Jul-2020 05:49:41

2K+ Views

The JSON is one of the widely used data-interchange formats and is a lightweight and language independent. The json.simple is a lightweight JSON processing library that can be used to encode or decode a JSON text. In the below program, we can convert a JSON String to Java object using the json.simple library.Exampleimport org.json.simple.*; import org.json.simple.parser.*; public class ConvertJSONStringToObjectTest {    public static void main(String[] args) {       String jsonString = "{\"Name\":\"Raja\", \"EmployeeId\":\"115\", \"Age\":\"30\"}";       JSONParser parser = new JSONParser();       JSONObject obj;       try {          obj = (JSONObject)parser.parse(jsonString);          System.out.println(obj.get("Name"));     ... Read More

Advertisements