How To Create Array Of Json Objects In Java
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
How to Write/create a JSON array using Java?
A Json array is an ordered collection of values that are enclosed in square brackets i.e. it begins with '[' and ends with ']'. The values in the arrays are separated by ',' (comma).
Sample JSON array
{ "books": [ Java, JavaFX, Hbase, Cassandra, WebGL, JOGL] } The json-simple is a light weight library which is used to process JSON objects. Using this you can read or, write the contents of a JSON document using Java program.
JSON-Simple maven dependency
Following is the maven dependency for the JSON-simple library −
<dependencies> <dependency> <groupId>com.googlecode.json-simple</groupId> <artifactId>json-simple</artifactId> <version>1.1.1</version> </dependency> </dependencies>
Paste this with in the <dependencies> </dependencies> tag at the end of your pom.xml file. (before </project> tag)
Example
To create an array in a JSON file using a Java program −
- Instantiate the JSONObject class of the json-simple library.
//Creating a JSONObject object JSONObject jsonObject = new JSONObject();
- Insert the required key-value pairs using the put() method of the JSONObject class.
jsonObject.put("key", "value"); - Create a JSON array by instantiating the JSONArray class and add, elements to the created array using the add() method of the JSONArray class.
JSONArray array = new JSONArray(); array.add("element_1"); array.add("element_2"); array.add("element_3"); - After adding all the required elements add the array into the JSON document using the put() method as −
jsonObject.put("contact",array); - Write the created JSON object into a file using the FileWriter class as −
FileWriter file = new FileWriter("E:/json_array_output.json"); file.write(jsonObject.toJSONString()); file.close(); Following Java program creates a JSON object with an array in it and writes it into a file named json_array_output.json.
Example
import java.io.FileWriter; import java.io.IOException; import org.json.simple.JSONArray; import org.json.simple.JSONObject; public class WritingJSONArray { public static void main(String args[]) { //Creating a JSONObject object JSONObject jsonObject = new JSONObject(); //Inserting key-value pairs into the json object jsonObject.put("ID", "1"); jsonObject.put("First_Name", "Krishna Kasyap"); jsonObject.put("Last_Name", "Bhagavatula"); jsonObject.put("Date_Of_Birth", "1989-09-26"); jsonObject.put("Place_Of_Birth", "Vishakhapatnam"); jsonObject.put("Country", "25000"); //Creating a json array JSONArray array = new JSONArray(); array.add("e-mail: krishna_kasyap@gmail.com"); array.add("phone: 9848022338"); array.add("city: Hyderabad"); array.add("Area: Madapur"); array.add("State: Telangana"); //Adding array to the json object jsonObject.put("contact",array); try { FileWriter file = new FileWriter("E:/json_array_output.json"); file.write(jsonObject.toJSONString()); file.close(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } System.out.println("JSON file created: "+jsonObject); } } Output
JSON file created: { "First_Name":"Krishna Kasyap", "Place_Of_Birth":"Vishakhapatnam", "Last_Name":"Bhagavatula", "contact":[ "e-mail: krishna_kasyap@gmail.com", "phone: 9848022338","city: Hyderabad", "Area: Madapur", "State: Telangana"], "Country":"25000", "ID":"1", "Date_Of_Birth":"1989-09-26"} If you observe the contents of the JSON file you can see the created data as −
Published on 03-Jul-2019 11:52:59
- Related Questions & Answers
- How to Write/create a JSON file using Java?
- How to create and write JSON array to a file in java?
- How to create a JSON Array using Object Model in Java?
- How to convert a JSON array to array using JSON-lib API in Java?
- How to read/parse JSON array using Java?
- How to convert a Collection to JSON Array using JSON-lib API in Java?
- How to convert an array to JSON Array using JSON-lib API in Java?
- How to create a JSON using Jackson Tree Model in Java?
- How to create a JSON Object using Object Model in Java?
- How to create a JSON using JsonObjectBuilder and JsonArrayBuilder in Java?
- How can we create a JSON using JsonGenerator in Java?
- How to write a JSON string to file using the Gson library in Java?
- How can we write JSON objects to a file in Java?
- How to convert Java Array/Collection to JSON array?
- How to convert JSON Array to normal Java Array?
How To Create Array Of Json Objects In Java
Source: https://www.tutorialspoint.com/how-to-write-create-a-json-array-using-java
Posted by: scotttheatione.blogspot.com

0 Response to "How To Create Array Of Json Objects In Java"
Post a Comment