How To Convert Json Array To String Array Inward Coffee - Gson Example
Sunday, November 4, 2018
Add Comment
JSON array is an ordered collection of values, which are enclosed inside brackets e.g. [] in addition to separated yesteryear a comma. In this Java tutorial, nosotros volition convert JSON Array to String array inwards Java in addition to after do JSON from Java String Array. This tutorial is similar to our final article inwards JSON about How to convert JSON object to Java object, instead of JSON object, hither nosotros volition convert JSON array to String array or List inwards Java. As I said earlier, at that topographic point are lot's of opened upwardly root library out at that topographic point which tin aid to parse JSON information format in addition to nosotros bring already seen Jackson library inwards our final example. In this tutorial, nosotros volition occupation GSON to parse JSON information format in addition to create Java String array or List from JSON array representation. Given the popularity of JSON every bit a lightweight choice to XML to transfer information from Server to customer inwards spider web applications, it's becoming imperative to know almost JSON information format in addition to parsing JSON string, much similar parsing XML documents in addition to knowing almost unlike XML parsers e.g. DOM or SAX.
Since Java application evolution is to a greater extent than almost reusing existing library, thence coding yourself, its of import to know what is available, in addition to which library other programmers are using. So far nosotros bring seen Jackson library, in addition to hither nosotros volition explore or thence other i called GSon.
Since Java application evolution is to a greater extent than almost reusing existing library, thence coding yourself, its of import to know what is available, in addition to which library other programmers are using. So far nosotros bring seen Jackson library, in addition to hither nosotros volition explore or thence other i called GSon.
How to convert JSON array to Java Array in addition to vice-versa
Here is the consummate code example. This Java instance uses GSON library to do List of String from JSON array in addition to farther Java criterion library to convert List to array. Instead of declaring JSON array inwards Code, which nosotros did hither for demonstration purpose, yous tin too read input from file, database or whatever URL. Code used to convert JSON array to coffee array volition rest same, except the getting input part. This JSON conversion example, too shows conversion of both String in addition to numeric JSON array to corresponding Java array.
import org.apache.log4j.Logger; import com.google.gson.Gson; import com.google.gson.reflect.TypeToken; /** * Java programme to convert JSON array to String array inwards Java or List. * * @author Javin Paul */ public class JsonArraytoJavaList { private static Logger logger = Logger.getLogger(JsonArraytoJavaList.class); public static void main(String args[]){ // Converting JSON String array to Java String array String jsonStringArray = "[\"JSON\",\"To\",\"Java\"]"; //creating Gson event to convert JSON array to Java array Gson converter = new Gson(); Type type = new TypeToken<List<String>>(){}.getType(); List<String> listing = converter.fromJson(jsonStringArray, type ); //convert List to Array inwards Java String [] strArray = list.toArray(new String[0]); logger.info("Java List created from JSON String Array - example"); logger.info("JSON String Array : " + jsonStringArray ); logger.info("Java List : " + list); logger.info("String array : " + Arrays.toString(strArray)); // let's straightaway convert Java array to JSON array - String toJson = converter.toJson(list); logger.info("Json array created from Java List : " + toJson); // instance to convert JSON int array into Java array in addition to List of integer String json = "[101,201,301,401,501]"; type = new TypeToken<List<Integer>>(){}.getType(); List<Integer> iList = converter.fromJson(json, type); Integer[] iArray = iList.toArray(new Integer[0]); logger.info("Example to convert numeric JSON array to integer List in addition to array inwards Java"); logger.info("Numeric JSON array : " + json); logger.info("Java List of Integers : " + iList); logger.info("Integer array inwards Java : " + Arrays.toString(iArray)); // Again, let's convert this Java int array dorsum to Json numeric array String toJsonInt = converter.toJson(iList); logger.info("numeric JSON array do from Java collection : " + toJsonInt); } } Output Java List created from JSON String Array - instance JSON String Array : ["JSON","To","Java"] Java List : [JSON, To, Java] String array : [JSON, To, Java] Json array created from Java List : ["JSON","To","Java"] Example to convert numeric JSON array to integer List in addition to array inwards Java Numeric JSON array : [101,201,301,401,501] Java List of Integers : [101, 201, 301, 401, 501] Integer array inwards Java : [101, 201, 301, 401, 501] numeric JSON array do from Java collection : [101,201,301,401,501]
Dependency
If yous are non using Maven for dependency management thence yous tin add gson-2.2.2.jar into your application's classpath, Otherwise yous tin add together next dependency inwards your projects pom.xml
<dependency>
<groupId>com.google.code.gson</groupId>
<artifactId>gson</artifactId>
<version>2.2.2</version>
</dependency>
That's all on How to convert JSON array to Java array in addition to collection. We bring seen instance of converting JSON String array to List of String in addition to String array inwards Java, similarly converting numeric JSON array to List of Integer in addition to integer array inwards Java. GSON library provides really convenient method e.g. toJson() in addition to fromJSon() which only postulate type information to perform conversion betwixt JSON in addition to Java.
Further Learning
Master Java Web Services in addition to REST API amongst Spring Boot
REST API Design, Development & Management
tutorial)
P.S. - If yous desire to acquire how to prepare RESTful Web Services using Spring Framework, cheque out Eugen Paraschiv's REST amongst Spring course. He has of late launched the certification version of the course, which is total of exercises in addition to examples to farther cement the existent globe concepts yous volition acquire from the course.
0 Response to "How To Convert Json Array To String Array Inward Coffee - Gson Example"
Post a Comment