[opencms-dev] JSON array from a list

Диканский Андрей Юрьевич adikanskii at ncfu.ru
Thu Dec 23 13:24:04 CET 2021


We made jsp file and use following code to return news items in json:


<%@page buffer="none" session="false" trimDirectiveWhitespaces="true" %>
<%@ taglib prefix="cms" uri="http://www.opencms.org/taglib/cms" %>
<%@ taglib prefix="fmt" uri="http://java.sun.com/jsp/jstl/fmt" %>
<%@ taglib prefix="fn" uri="http://java.sun.com/jsp/jstl/functions" %>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>

<%@page import="javax.servlet.http.HttpServletResponse,org.opencms.flex.CmsFlexController,org.apache.commons.lang3.StringEscapeUtils" %>
<%
HttpServletResponse topLevelResponse = CmsFlexController.getController(request).getTopResponse();
topLevelResponse.setHeader("Content-Type","application/json;charset=UTF-8");
topLevelResponse.setHeader("Access-Control-Allow-Origin", "*");
%>

<c:set var="searchPath">${cms.requestContext.siteRoot}/</c:set>
<c:set var='solrNewsQuery'>
{
"core" : "Solr Online",
"ignorequery" : true,
"ignoreReleaseDate" : false,
"ignoreExpirationDate" : false,
"queryparam" : ":",
"escapequerychars" : true,
"extrasolrparams" : "fq=parent-folders:\"${searchPath}\"&fq=type:NewsBlock&fq=category:Glavnaya&sort=Newsdate_prop desc",
"pagesize" : "10",
}
</c:set>
<cms:search configString="${solrNewsQuery}" var="search" addContentInfo="true" />
[
<c:forEach var="news" items="${search.searchResults}" varStatus="status">
<c:set var="convertId">${news.xmlContent.id}</c:set>
<c:set var="convertTitle">${news.xmlContent.value.Title}</c:set>
<c:set var="convertDescription">${news.xmlContent.value.ShortDescription}</c:set>
<c:set var="convertFullDescription">${news.xmlContent.value.FullDescription}</c:set>
<c:set var="convertPrewImg">https://www.mysite.ru<cms:link>${news.xmlContent.value.ImagePreview}</cms:link></c:set>
<c:set var="convertUrl">https://www.mysite.ru<cms:link>${news.xmlContent.filename}</cms:link></c:set>
<fmt:setLocale value="ru-RU"/>
<fmt:timeZone value="GMT+3:00">
<fmt:formatDate type = "both" dateStyle = "long" timeStyle = "long" value = "${cms:convertDate(news.xmlContent.value.Date)}" var="fullDate"/>
<c:set var="formatedDateArray" value='${fn:split(fullDate, " ")}' />
</fmt:timeZone>
<c:set var="convertDate">${formatedDateArray[0]} ${formatedDateArray[1]} ${formatedDateArray[2]}</c:set>
{
"id" : "<%=StringEscapeUtils.escapeEcmaScript(pageContext.getAttribute("convertId").toString())%>",
"date" : "<%=StringEscapeUtils.escapeEcmaScript(pageContext.getAttribute("convertDate").toString())%>",
"title": "<%=StringEscapeUtils.escapeEcmaScript(pageContext.getAttribute("convertTitle").toString())%>",
"short_description": "<%=StringEscapeUtils.escapeEcmaScript(pageContext.getAttribute("convertDescription").toString())%>",
"full_description": "<%=StringEscapeUtils.escapeEcmaScript(pageContext.getAttribute("convertFullDescription").toString().replaceAll("src=\"","src=\"https://www.mysite.ru").replaceAll("\\<iframe[^>]*>","").replaceAll("\\<a[^>]*>",""))%>",
"url": "<%=StringEscapeUtils.escapeEcmaScript(pageContext.getAttribute("convertUrl").toString())%>",
"img": "<%=StringEscapeUtils.escapeEcmaScript(pageContext.getAttribute("convertPrewImg").toString())%>"
}${status.last ? "" : ","}
</c:forEach>
]



________________________________
От: opencms-dev <opencms-dev-bounces at opencms.org> от имени Paoletti Corrado <c.paoletti at fineco.it>
Отправлено: 23 декабря 2021 г. 15:15
Кому: opencms-dev at opencms.org
Тема: [opencms-dev] JSON array from a list

FinecoBank S.p.A. - Internal Use Only
Hi,
my target is to generate a JSON response from a list of users.

With one user <cms:jsonpart generates a correct JSON but when I use it inside a <c:forEach I obtain only the last user so I deduce that <cms:jsonpart is not sufficient.

Searching examples and documentation I find that I could use a combination of JSONArray and JSONObject and these objects are defined in cms taglibrary (https://documentation.opencms.org/javadoc/tld/cms/jsonarray.html)

So, I have write <cms:jsonarray but I have:

No tag "jsonarray" defined in tag library imported with prefix "cms"

Someone could suggest me the right way to follow to have a JSON array like this:

[
{"name":"giulio", "surname":"cesare"},
{"name":"dante", "surname":"alighieri"},
]

Another approach is to use Java (I am not Java programmer) so I have wrote:

<%@page import="org.opencms.json.JSONException, org.opencms.json.JSONArray, org.opencms.json.JSONObject"%>

<%
    JSONObject user = new JSONObject();
    try {
        user.put("name", "giulio");
        user.put("surname", "cesare");
    } catch (JSONException e) {
    }
    JSONArray userList = new JSONArray();
    userList.put(user);

        System.out.println( userList );
%>

but nothing happens.

Kind regards and Happy Christmas.

Corrado

-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.opencms.org/pipermail/opencms-dev/attachments/20211223/2a6ee957/attachment.htm>


More information about the opencms-dev mailing list