[opencms-dev] JSON array from a list

Tobias Karrer kartobi at gmail.com
Thu Dec 23 13:45:06 CET 2021


Hi Corrado,


I have good experiences using this taglib:
<%@ taglib uri="http://www.atg.com/taglibs/json" prefix="json" %>

E.g.
         <json:object>
           <json:array name="groups">
             <c:forEach items="${content.valueList.Groups}" var="group">
               <json:object>
                 <json:property name="name" value="${group.value.Name}"/>
                 <json:property name="count" value="${group.value.Count}"/>
                 <json:array name="Tags">
                   <c:set var="tag">${group.value.Tags}</c:set>
                   <c:forEach items="${fn:split(tags, ',')}" var="tag">
                     <json:property value="${tag}"/>
                   </c:forEach>
                 </json:array>
                 <json:property name="filter" 
value="${group.value.Filter}"/>
               </json:object>
             </c:forEach>
           </json:array>
         </json:object>


BR, Tobias


Am 23.12.2021 um 12:24 schrieb Диканский Андрей Юрьевич:
>
> 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
>
> _______________________________________________
> This mail is sent to you from the opencms-dev mailing list
> To change your list options, or to unsubscribe from the list, please visit
> https://lists.opencms.org/mailman/listinfo/opencms-dev
>
>
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.opencms.org/pipermail/opencms-dev/attachments/20211223/2f46bd99/attachment.htm>


More information about the opencms-dev mailing list