yomna esam
yomna esam

Reputation: 97

convert list of lists to json object in java

I have table employee which has the columns EMPID, EMPNAME, EMPAGE, SALARY, ADDRESS, department_id (foreign key to department_id in department) and another table department which has department_id, name. So I made a query

List<Department> deps = sessionFactory.getCurrentSession().createCriteria(Department.class).list(); 

The return vlaue is a List of Lists

I want to put the result in in JSON Object. How can I achieve this?

Upvotes: 1

Views: 2600

Answers (2)

sainiVinay
sainiVinay

Reputation: 1

if you are using Spring then just add produces ={"application/json"} in your REST @RequestMapping and rest thing Spring will do for you

Upvotes: 0

Herr Derb
Herr Derb

Reputation: 5387

Do something like this

    List<List<Object>> listOfLists = new ArrayList<>();
    JSONArray jsonArray = new JSONArray();
        for (List<Object> list : listOfLists) {
        JSONArray newArray = new JSONArray(list);
        jsonArray.put(newArray);
    }

Upvotes: 1

Related Questions