在plpsql中,我试图将响应作为JSON返回

时间:2017-01-30 09:00:29

标签: postgresql

IF (skip = 0) THEN
    SELECT COUNT(*) as totalcount
    FROM table1 c
    INNER JOIN table2 n ON n.chapter = c.id
    WHERE c.revision = revisionId
        AND n.data IS NOT NULL
        AND n.type = 'text'
        AND data ILIKE query INTO totalcount;
  END IF;

  response.totalcount := totalcount;

如何返回json,就像这里:

{ totalcount: 5, skip: 52, result: [{data: "book1", type: chemistry}, {data: "book2", type: physics}]}

1 个答案:

答案 0 :(得分:0)

尝试将其包装在外部查询中并使用SELECT to_json(wrap) FROM (SELECT ...) wrap;

List<String> list = new ArrayList<>();
try {
    String result = new ObjectMapper().writerFor(new TypeReference<List<String>>(){}).writeValueAsString(list);
} catch (JsonProcessingException e) {
    e.printStackTrace();
}