How to convert Java Array to JSON array?
In this Java tutorial, you gonna learn how to convert Java Array to JSON array. In other programming languages, it is easy enough to work with JSON array and much easier. Because some languages are providing the inbuilt functions to work with JSON.
But fortunately, we are lucky enough that Google has provided a library named org.json.JSONArray
JSONArray() This method Constructs an empty JSONArray.
Also read,
How to convert JSON array to normal Java Array Easily
How to remove null values from a String array in Java in various ways
Convert Java Array to JSON array with an example
In order to add this library to your project, you will need to create a maven dependency.
<dependency> <groupId>com.googlecode.json-simple</groupId> <artifactId>json-simple</artifactId> <version>1.1</version> </dependency>
Again you are having a very useful method put()
Using this method you can add array elements into the JSONArray
Let’s take a look at this below example
import org.json.JSONArray; public class MyClass { public static void main(String[] args) { String [] normal_array = {"CodeSpeedy", "JSON Array", "Java Array", "Saruque"}; JSONArray json_array= new JSONArray(); for (int i = 0; i < normal_array.length; i++) { json_array.put(normal_array[i]); } System.out.println(json_array); } }
Output:
["CodeSpeedy","JSON Array","Java Array","Saruque"]
How to convert Byte Array to Image in java with easy example
If you want to convert Java ArrayList into JSON array then you may follow the below example:
import java.util.ArrayList; import org.json.JSONArray; public class MyClass { public static void main(String[] args) { ArrayList <String> my_arraylist= new ArrayList<String>(); my_arraylist.add("CodeSpeedy"); my_arraylist.add("JSON Array"); my_arraylist.add("Java Array"); my_arraylist.add("Saruque"); JSONArray json_array = new JSONArray(arrayList); System.out.println(json_array); } }
Output:
["CodeSpeedy","JSON Array","Java Array","Saruque"]
It is easier to work with ArrayList because you don’t need to use put() method in a for loop to add the array elements.
How to convert JSON array to normal Java Array Easily
How to convert BLOB to Byte Array in java
Parse this json data in java, Want to know it’ source code. Please do
{“data”:[{“header”:”center:@:ENVELOP_ID:@:center_city:@:dispatch_date”},{“value”:”:@::@::@:”},{“value”:”[[HEADERTITLE]] e 101 achla”},{“value”:”:@::@::@:”},{“value”:”101:@:14:@:City 1:@:05/07/2019″},{“value”:”:@:15:@:City 1:@:05/07/2019″},{“value”:”:@:16:@:City 1:@:05/07/2019″},{“value”:”:@:17:@:City 1:@:05/07/2019″},{“value”:”:@:18:@:City 1:@:05/07/2019″},{“value”:”:@:19:@:City 1:@:05/07/2019″},{“value”:”:@:20:@:City 1:@:05/07/2019″},{“value”:”:@:21:@:City 1:@:05/07/2019″},{“value”:”:@:22:@:City 1:@:05/07/2019″},{“value”:”:@:23:@:City 1:@:05/07/2019″},{“value”:”:@::@::@:”},{“value”:”[[HEADERTITLE]] h 102 aahl”},{“value”:”:@::@::@:”},{“value”:”102:@:16:@:City 2:@:06/07/2019″},{“value”:”:@:17:@:City 2:@:06/07/2019″},{“value”:”:@:18:@:City 2:@:06/07/2019″},{“value”:”:@:19:@:City 2:@:06/07/2019″},{“value”:”:@:20:@:City 2:@:06/07/2019″},{“value”:”:@:21:@:City 2:@:06/07/2019″},{“value”:”:@:22:@:City 2:@:06/07/2019″},{“value”:”:@:23:@:City 2:@:06/07/2019″},{“value”:”:@:24:@:City 2:@:06/07/2019″},{“value”:”:@::@::@:”},{“value”:”[[HEADERTITLE]] c 103 bbgv “},{“value”:”:@::@::@:”},{“value”:”103:@:12:@:City 3:@:07/07/2019″},{“value”:”:@:13:@:City 3:@:07/07/2019″},{“value”:”:@:14:@:City 3:@:07/07/2019″},{“value”:”:@:15:@:City 3:@:07/07/2019″},{“value”:”:@:16:@:City 3:@:07/07/2019″},{“value”:”:@:17:@:City 3:@:07/07/2019″},{“value”:”:@:18:@:City 3:@:07/07/2019″},{“value”:”:@:19:@:City 3:@:07/07/2019″},{“value”:”:@::@::@:”},{“value”:”[[HEADERTITLE]] s 104 eedg “},{“value”:”:@::@::@:”},{“value”:”104:@:99:@:City 4:@:08/07/2019″},{“value”:”:@:100:@:City 4:@:08/07/2019″},{“value”:”:@:101:@:City 4:@:08/07/2019″},{“value”:”:@:102:@:City 4:@:08/07/2019″},{“value”:”:@:103:@:City 4:@:08/07/2019″},{“value”:”:@:104:@:City 4:@:08/07/2019″},{“value”:”:@:105:@:City 4:@:08/07/2019″},{“value”:”:@::@::@:”},{“value”:”[[HEADERTITLE]] p 105 ffgb”},{“value”:”:@::@::@:”},{“value”:”105:@:77:@:City 5:@:09/07/2019″},{“value”:”:@:78:@:City 5:@:09/07/2019″},{“value”:”:@:79:@:City 5:@:09/07/2019″},{“value”:”:@:80:@:City 5:@:09/07/2019″},{“value”:”:@:81:@:City 5:@:09/07/2019″},{“value”:”:@:82:@:City 5:@:09/07/2019″}],”status”:”success”}