Jumat, 11 Juli 2014

How to Convert JSON array to String array in Java - GSon example




JSON array is a ordered collection of values, which are enclosed within
brackets e.g.
[] and separated by comma. In this Java tutorial
we will convert JSON Array to String array in Java and subsequently create JSON
from Java String Array. This tutorial is similar to our last article in JSON
about How to convert JSON object to Java object,
i
nstead of JSON object, here we will convert JSON array to String array or List
in Java. As I said earlier, there are lot's of open source library out there
which can help to parse JSON data format and we have already seen Jackson
library in our last example. In this tutorial we will use GSON to parse JSON
data format and create Java
String array
or List
from JSON array representation. Given the popularity of JSON as lightweight alternative of XML to transfer data from Server to client in web applications, it's becoming imperative to know about JSON data format and parsing JSON string, much like parsing XML documents and knowing about different XML parsers e.g. DOM or SAX. Since Java application development is more about reusing existing library, then coding yourself, its important to know what is available, and which library other programmers are using. So far we have seen Jackson library, and here we will explore another one called GSon.






How to convert JSON array to Java Array and
vice-versa



Here is the complete code example. This Java example uses GSON library to
create List of String from JSON array and further Java standard library to convert
List to array
. Instead of declaring JSON array in Code, which we did here for demonstration purpose, you can also read input from file, database or any URL. Code used to convert JSON array to java array will remain same, except the getting input part. This JSON conversion example, also shows conversion of both String and numeric JSON array to corresponding Java array.




import org.apache.log4j.Logger;

import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;

/**
* Java program to convert JSON array to String array in 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 instance to convert JSON array to Java array
Gson converter = new Gson();

Type type = new TypeToken<List<String>>(){}.getType();
List<String> list = converter.fromJson(jsonStringArray, type );

//convert List to Array in 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 now convert Java array to JSON array -
String toJson = converter.toJson(list);
logger.info("Json array created from Java List : " + toJson);

// example to convert JSON int array into Java array and 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 and array in Java");
logger.info("Numeric JSON array : " + json);
logger.info("Java List of Integers : " + iList);
logger.info("Integer array in Java : " + Arrays.toString(iArray));

// Again, let's convert this Java int array back to Json numeric array
String toJsonInt = converter.toJson(iList);
logger.info("numeric JSON array create from Java collection : " + toJsonInt);
}
}

Output
Java List created from JSON String Array - example
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 and array in Java
Numeric JSON array : [101,201,301,401,501]
Java List of Integers : [101, 201, 301, 401, 501]
Integer array in Java : [101, 201, 301, 401, 501]
numeric JSON array create from Java collection : [101,201,301,401,501]





Dependency


If you are not using Maven for dependency management then you can
add 
gson-2.2.2.jar into your application's classpath,
Otherwise you can add following dependency in your projects pom.xml








  com.google.code.gson


  gson


  2.2.2






That's all on How to convert JSON array to Java array and collection.
We have seen example of converting JSON String array to List of String and
String array in Java, similarly converting numeric JSON array to List of
Integer and integer array in Java. GSON library provides very convenient
method e.g.
toJson() and fromJSon() which just
need type information to perform conversion between JSON and Java.





























Source:http://javarevisited.blogspot.com/2013/04/convert-json-array-to-string-array-list-java-from.html

Tidak ada komentar:

Posting Komentar