I have a function which is returning Data as List
in java class. Now as per my need, I have to convert it into Json
Format.
Below is my function code snippet:
public static List<Product> getCartList() {
List<Product> cartList = new Vector<Product>(cartMap.keySet().size());
for(Product p : cartMap.keySet()) {
cartList.add(p);
}
return cartList;
}
I tried To convert into json
by using this code but it is giving type mismatch error as function is of type List...
public static List<Product> getCartList() {
List<Product> cartList = new Vector<Product>(cartMap.keySet().size());
for(Product p : cartMap.keySet()) {
cartList.add(p);
}
Gson gson = new Gson();
// convert your list to json
String jsonCartList = gson.toJson(cartList);
// print your generated json
System.out.println("jsonCartList: " + jsonCartList);
return jsonCartList;
}
Please help me resolve this.
JSONObject obj = new JSONObject(); List<String> sList = new ArrayList<String>(); sList. add("val1"); sList. add("val2"); obj. put("list", sList);
Put a key/value pair in the JSONObject, where the value will be a JSONArray which is produced from a Collection. But it puts JSON representation of your object as String. So you should use either JSONArrays or Java objects. You should not mix them.
A Gson is a library that can be used to convert Java Objects to JSON representation.
We can convert a List to JSON array using the writeValueAsString() method of ObjectMapper class and this method can be used to serialize any Java value as a String.
The ObjectMapper class is the most important class in the Jackson API that provides readValue () and writeValue () methods to transform JSON to Java Object and Java Object to JSON. We can convert a List to JSON array using the writeValueAsString () method of ObjectMapper class and this method can be used to serialize any Java value as a String.
These are the following steps to convert a Java object into JSON: 1 Create a Maven project. 2 Add GSON dependency in xml file. 3 Create Plain Old Java Object to convert into JSON. 4 Create a Java class to convert the Java object to JSON. More ...
The Jackson library is used to serialize the Java object to JSON and vice versa. The ObjectMapper class of the Jackson API provides methods to convert the Java object to JSON format or object. The ObjectMapper class writeValueAsString () method takes the JSON object as a parameter and returns its respective JSON string.
JSONListConverter Class: JSONListConverter class is responsible for performing following operations. Convert List of Person objects to JSON String in java.
Using gson it is much simpler. Use following code snippet:
// create a new Gson instance
Gson gson = new Gson();
// convert your list to json
String jsonCartList = gson.toJson(cartList);
// print your generated json
System.out.println("jsonCartList: " + jsonCartList);
// Converts JSON string into a List of Product object
Type type = new TypeToken<List<Product>>(){}.getType();
List<Product> prodList = gson.fromJson(jsonCartList, type);
// print your List<Product>
System.out.println("prodList: " + prodList);
public static List<Product> getCartList() {
JSONObject responseDetailsJson = new JSONObject();
JSONArray jsonArray = new JSONArray();
List<Product> cartList = new Vector<Product>(cartMap.keySet().size());
for(Product p : cartMap.keySet()) {
cartList.add(p);
JSONObject formDetailsJson = new JSONObject();
formDetailsJson.put("id", "1");
formDetailsJson.put("name", "name1");
jsonArray.add(formDetailsJson);
}
responseDetailsJson.put("forms", jsonArray);//Here you can see the data in json format
return cartList;
}
you can get the data in the following form
{
"forms": [
{ "id": "1", "name": "name1" },
{ "id": "2", "name": "name2" }
]
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With