Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to convert list data into json in java

Tags:

java

json

list

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.

like image 274
vikas Avatar asked Mar 11 '13 06:03

vikas


People also ask

How do I add a list to a JSON object in Java?

JSONObject obj = new JSONObject(); List<String> sList = new ArrayList<String>(); sList. add("val1"); sList. add("val2"); obj. put("list", sList);

How can we send list of objects in JSON?

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.

Can GSON convert list to JSON?

A Gson is a library that can be used to convert Java Objects to JSON representation.

How do I convert a JSON list to Jackson?

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.

How to convert a list to JSON array in Jackson?

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.

How to convert a Java object to JSON?

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 ...

How to serialize Java object to JSON in Jackson?

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.

What is the use of @jsonlistconverter class?

JSONListConverter Class: JSONListConverter class is responsible for performing following operations. Convert List of Person objects to JSON String in java.


2 Answers

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);

Converting back from JSON string to your Java object

 // 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);
like image 84
anubhava Avatar answered Oct 24 '22 00:10

anubhava


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" } 
    ]
}
like image 20
PSR Avatar answered Oct 23 '22 23:10

PSR