Found 206 Articles for JSON

How can we parse a nested JSON object in Java?

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

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

How to construct a JSON object from a subset of another JSON object in Java?

raja
Updated on 04-Jul-2020 05:50:07

3K+ Views

The JSON stands for JavaScript Object Notation and it can be used to transfer and storage of data. The JSONObject can parse text from a String to produce a map-like object. We can also construct a JSON object from a subset of another JSON object using the JSONObject(JSONObject jo, java.lang.String[] names) constructor, an array of strings is used to identify the keys that can be copied and missing keys are ignored.Syntaxpublic JSONObject(JSONObject jo, java.lang.String[] names)Exampleimport java.util.*; import org.json.*; public class JSONSubsetTest {    public static void main(String[] args) throws JSONException {       Map map = new HashMap();     ... Read More

How can we merge two JSON arrays in Java?

raja
Updated on 04-Jul-2020 05:51:07

6K+ Views

A JSON is a lightweight data-interchange format and the format of JSON is a key with value pair. The JSONArray can parse text from a String to produce a vector-like object and supports java.util.List interface. We can use org.json.simple.JSONArray class to merge two JSON arrays in Java.We can merge two JSON arrays using the addAll() method (inherited from interface java.util.List) in the below program.Exampleimport org.json.simple.JSONArray; import java.io.IOException; public class MergeJSONArraysTest {    public static void main(String[] args) throws IOException {       JSONArray jsonArray1 = new JSONArray(); // first json array       jsonArray1.add("Java");       jsonArray1.add("Python");       jsonArray1.add("Spark");     ... Read More

How can we convert a map to the JSON object in Java?

raja
Updated on 04-Jul-2020 05:51:35

8K+ 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 Map to JSON object using the toJSONString() method(static) of org.json.simple.JSONValue. It has two important static methods: writeJSONString() method to encode an object into JSON text and write it out, escape() method to escape the special characters and escape quotes,  \, /, \r, , \b, \f, \t.Exampleimport java.util.*; import org.json.simple.JSONValue; public class ConvertMapJSONTest {    public static void main(String[] args) {       ... Read More

How can we merge two JSON objects in Java?

raja
Updated on 04-Jul-2020 05:40:36

13K+ 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 use org.json.simple.JSONObject to merge two JSON objects in Java.We can merge two JSON objects using the putAll() method (inherited from interface java.util.Map) in the below program.Exampleimport java.util.Date; import org.json.simple.JSONObject; public class MergeJsonObjectsTest {    public static void main(String[] args) {       JSONObject jsonObj = new JSONObject(); // first json object       jsonObj.put("Name", "Adithya");       jsonObj.put("Age", 25);     ... Read More

How can we write JSON objects to a file in Java?

raja
Updated on 04-Jul-2020 05:35:31

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 write JSON files and it can be used to encode or decode JSON text and fully compliant with JSON specification(RFC4627). In order to read a JSON file, we need to download the json-simple.jar file and set the path to execute it.Exampleimport java.io.*; import java.util.*; import org.json.simple.*; import org.json.simple.parser.*; public class JSONObjectWriterToFileTest {    public static void main(String[] args) throws IOException {       JSONObject obj = new JSONObject();       obj.put("Name", "Adithya");       ... Read More

How can we add a JSONArray to JSONObject in Java?

raja
Updated on 04-Jul-2020 05:36:04

15K+ Views

The JSON is a text-based format for exchanging data. It is a lightweight component and language independent. We can also add a JSONArray to JSONObject. We need to add a few items to an ArrayList first and pass this list to the put() method of JSONArray class and finally add this array to JSONObject using the put() method. Exampleimport org.json.*; import java.util.*; public class AddJSONArrayToJSONObjTest {    public static void main(String args[]) {       List list = new ArrayList();       list.add("Raja");       list.add("Jai");       list.add("Adithya");       JSONArray array = new JSONArray();       for(int i = 0; i < list.size(); ... Read More

How can we read a JSON file in Java?

raja
Updated on 02-Sep-2023 12:55:51

49K+ 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 read and write JSON files and it can be used to encode or decode JSON text and fully compliant with JSON specification (RFC4627). In order to read a JSON file, we need to download the json-simple.jar file and set the path to execute it.json fileExampleimport java.io.*; import java.util.*; import org.json.simple.*; import org.json.simple.parser.*; public class JSONReadFromTheFileTest {    public static void main(String[] args) {       JSONParser parser = new JSONParser();       try {     ... Read More

Advertisements