What then is an Array? An array is a data structure in java that holds values of the same type with its length specified right from creation time. Or you can say an array is a collection of similar type of elements which has contiguous memory location.
Oftentimes you may have to work on a task whereby you will have to store a large number of values during the execution of a program. for instance, you need to read 50 numbers, calculate their average, and find out how many numbers are above the average, and how many numbers are below average.
You have to declare 50 variables and repeatedly write almost identical code 50 times. Writing a program this way would be impractical. So, how do you solve this problem? An efficient, organized approach is needed.
How do you create an array in Java?
In these previous lines of codes, you are creating an array of integer numbers with the variable name “num” and you are assigning it to a new integer of length 5. meaning that the items coming can only be an integer and it has to be 5.
If you want to get the total number of the index you will do length – 1 because the length you specified at creation time is 5 and because the indexes start from 0, not 1. so, the length of the indexes is 4.
What is JSON?
It is a common data format with diverse uses in electronic data interchange, including that of web applications with servers. JSON is a language-independent data format.
Fig 1.0: How Json works with the server.
In JSon, note that:
·Data is in name/value pairs
·Data is separated by commas
·Curly braces hold objects
·Square brackets hold arrays
Array is a type of data Structure, so Imagine you have an array with
object notation (Json). The Implementation gives how to do that.
How to convert array to JSON in Java
Now we would be solving a problem which is converting an array into json in java.
1 declares the class ArrayToJson and with the main method in line 2. an
array of numbers of type int was initialized with a set of values in
line 3 and in line 4, arrays of fruits of type String was initialized
too. Line 6 creates a new instance of Gson.
Note: Gson is a class in
java with its inbuilt methods. So gson was created as an instance of
Gson. Line 8 created a variable with the name “numbersJson” and the
method toJson was called which takes in numbers as argument and was
stored in the variable that was just created. The same thing applies to
the fruits too. Line 11 and 12 prints the results out.
numbersJson = [1,1,2,3,5,8,13] daysJson = ["Apple", "Cucumber", "Pineapple", "Grape", "Orange", "Pawpaw", "Water melon"]
That’s all about how to convert an array to JSON in Java. You can see by using Gson API and library it’s quite easy to convert any type of Java array to JSON messages.
Other Java JSON tutorials you may like
- How to convert JSON String to Java Object? (example)
- How to use Google Protocol Buffer in Java? (tutorial)
- How to convert JSON array to String array in Java? (answer)
- Top 10 RESTful Web Service Interview Questions (see here)
- How to parse large JSON files using Jackson Streaming API? (example)
- 5 Books to Learn REST and RESTful Web Services (books)
- How to consume JSON from RESTful Web Services using RestTemplate of Spring? (tutorial)
- What is the purpose of different HTTP methods in REST? (see here)
- Top 5 Courses to learn REST with Java? (courses)
- How to convert JSON to HashMap and vice-versa (tutorial)
Thanks for reading this article. If you like this article then please
share it with your friends and colleagues, if you have any questions or
feedback then please drop a comment.
P.S. – If you want to learn more about the advanced topic in Java, I also suggest you join these Free Java Programming courses, which cover several advanced Java features like JSON, RESTFul Web Services, JAXB, JDBC, etc.