将List< Object[]>转换为JSON

v440hwme  于 12个月前  发布在  其他
关注(0)|答案(2)|浏览(129)

大家好

有人可以帮助我,如何将我的HQL查询结果转换为JSON与对象列表,并得到它与休息服务。

这里是我的服务方法,返回qyery结果列表:

@Override
    public List<Object[]> getAllDepartments() {
        List<Object[]> list;
        Query query =  entityManager.createQuery("SELECT dp.name, avg (wr.salary) FROM Worker wr join wr.department dp GROUP BY dp.name");
        list =  query.getResultList();
        return list;
    }

字符串

我的Spring RESTfull控制器:

@RequestMapping(value = "/all", method = RequestMethod.GET)
@ResponseBody
public JsonNode getAllDeps() {
    List<Object[]> list  = departmentService.getAllDepartments();
    ObjectMapper objectMapper = new ObjectMapper();
    Map<String, Integer> resultMap = new HashMap<String, Integer>(list.size());
    for (Object[] result : list)
        resultMap.put((String)result[0], ((Double)result[1]).intValue() );
    final JsonNode json = objectMapper.valueToTree(resultMap);
    return json;
}


现在我的服务响应我的json与数据在那看起来:{“安全”:1500,“经济”:1850,“IT”:2000}
但我需要在那个(对象列表):
[{name:“Security”,salary:“1500”},{name:“Economical”,salary:1850},{name:“IT”,salary:2000}]
谢谢你的帮助。

bpzcxfmw

bpzcxfmw1#

只需从控制器方法返回列表

@RequestMapping(value = "/all", method = RequestMethod.GET)
@ResponseBody
public List<Object[]> getAllDeps() {
    List<Object[]> list  = departmentService.getAllDepartments();
    return list;
}

字符串
@ResponseBody注解为您完成转换。

91zkwejq

91zkwejq2#

public JSONObject findOccurrByFileUUID(UUID filePefin) throws JSONException {

    StringBuilder strQuery = new StringBuilder();
    strQuery.append("SELECT c.\"name\" AS creditorname, d.\"name\" AS debtorname, concat(b2.\"type\",'-',b2.\"number\",'-',b2.parcel) AS docto, b2.uniquenumber, to_char(b.dateoccurr,'DD/MM/YYYY HH24:MI:SS') AS dateoccur, to_char(b.createddate, 'DD/MM/YYYY HH24:MI:SS') AS createddate, b.statusnegatived, l.username FROM billnegativedoccurr b \n" +
                    "LEFT JOIN billnegatived b2 ON b2.uuid = b.billnegativeduuid \n" +
                    "LEFT JOIN creditor c ON c.tenantowner_uuid = b2.tenant_uuid AND c.uuid = b2.creditor_uuid \n" +
                    "LEFT JOIN debtor d ON d.tenantowner_uuid = b2.tenant_uuid AND d.uuid = b2.debtor_uuid \n" +
                    "INNER JOIN login l ON l.uuid = b.useroccurr \n" +
                    "WHERE b.filepefinuuid = :filePefin");

    Query query = eM.createNativeQuery(strQuery.toString()); //no entity mapping
    query.setParameter("filePefin", filePefin);
    List<Object[]> queryList = query.getResultList();
    JSONArray array = new JSONArray();
    JSONObject obj = new JSONObject();

    for (Object[] result : queryList) {

        JSONObject object = new JSONObject();
        object.put("creditorName", result[0]);
        object.put("debtorName", result[1]);
        object.put("docto", result[2]);
        object.put("uniquenumber", result[3]);
        object.put("dateoccur", result[4]);
        object.put("createddate", result[5]);
        object.put("statusnegatived", result[6]);
        object.put("username", result[7]);

        array.put(object);
    }

    return obj.put("Values", array);
}

字符串

相关问题