How to convert generic list to json in Java.I have class like this..
public class Output
{
public int Keyname { get; set; }
public Object outputvalue{ get; set; } //outvalue may be even a object collection
}
List<Output> outputList = new List<Output>();
I want to convert outputList into json in Java.After converting i will send it to client.
You need an external library for this.
JSONArray jsonA = JSONArray.fromObject(mybeanList);
System.out.println(jsonA);
Google GSON is one of such libraries
You can also take a look here for examples on converting Java object collection to JSON string.
Try this:
public void test(){
// net.sf.json.JSONObject, net.sf.json.JSONArray
List objList = new ArrayList();
objList.add("obj1");
objList.add("obj2");
objList.add("obj3");
HashMap objMap = new HashMap();
objMap.put("key1", "value1");
objMap.put("key2", "value2");
objMap.put("key3", "value3");
System.out.println("JSONArray :: "+(JSONArray)JSONSerializer.toJSON(objList));
System.out.println("JSONObject :: "+(JSONObject)JSONSerializer.toJSON(objMap));
}
you can find API here.
download java-json.jar from Java2s then use the JSONArray constructor
List myList = new ArrayList<>();
JSONArray jsonArray = new JSONArray(myList);
System.out.println(jsonArray);
jackson provides very helpful and lightweight API to convert Object to JSON and vise versa. Please find the example code below to perform the operation
List<Output> outputList = new ArrayList<Output>();
public static void main(String[] args) {
try {
Output output = new Output(1,"2342");
ObjectMapper objectMapper = new ObjectMapper();
String jsonString = objectMapper.writeValueAsString(output);
System.out.println(jsonString);
} catch (JsonProcessingException e) {
e.printStackTrace();
}
}
there are many other features and nice documentation for Jackson API. you can refer to the links like: https://www.journaldev.com/2324/jackson-json-java-parser-api-example-tutorial..
dependencies to include in the project are
<!-- Jackson -->
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-databind</artifactId>
<version>2.5.1</version>
</dependency>
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-core</artifactId>
<version>2.5.1</version>
</dependency>
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-annotations</artifactId>
<version>2.5.1</version>
</dependency>
Use GSONBuilder with setPrettyPrinting and disableHtml for nice output.
String json = new GsonBuilder().setPrettyPrinting().disableHtmlEscaping().
create().toJson(outputList );
fileOut.println(json);
For simplicity and well structured sake, use SpringMVC. It's just so simple.
@RequestMapping("/carlist.json")
public @ResponseBody List<String> getCarList() {
return carService.getAllCars();
}
Reference and credit: https://github.com/xvitcoder/spring-mvc-angularjs
Look at the google gson library. It provides a rich api for dealing with this and is very straightforward to use.
Source: Stackoverflow.com