简体   繁体   中英

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?

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

如果您使用的是 Spring,那么只需在您的 REST @RequestMapping添加produces ={"application/json"}并休息 Spring 将为您做的事情

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM