0

我想将这些隐藏输入的值组合成一个单独的隐藏输入中的 json 格式字符串。我如何实现这一目标?

<c:forEach var="perForm" items="${importedPersonForms}" varStatus="count">
    <input type="hidden" name="importedPersonForms[${count.count - 1}].firstname" value="${perForm.firstname}"/>
    <input type="hidden" name="importedPersonForms[${count.count - 1 }].lastname" value="${perForm.lastname}"/>
    <c:forEach var="position" items="${perForm.positions}">
        <input type="hidden" name="importedPersonForms[${count.count - 1 }].positions" value="${position}"/>
    </c:forEach>
    <c:forEach var="organization" items="${perForm.organizations}">
        <input type="hidden" name="importedPersonForms[${count.count - 1 }].organizations" value="${organization}"/>
    </c:forEach>
    <input type="hidden" name="importedPersonForms[${count.count - 1 }].addressList[0].countryCode" value="${perForm.addressList[0].countryCode}"/>
    <input type="hidden" name="importedPersonForms[${count.count - 1 }].addressList[0].number" value="${perForm.addressList[0].number}"/>
    <input type="hidden" name="importedPersonForms[${count.count - 1 }].addressList[0].fixedLine" value="${perForm.addressList[0].fixedLine}"/>
</c:forEach>
4

1 回答 1

0

我相信下面的代码会解决你的问题。
变量json将是一个 JSONArray,您可以将其作为值添加到隐藏的输入

<c:set var="json" value="{["/>
<c:forEach var="perForm" items="${importedPersonForms}" varStatus="count">

    <c:set var="json" value="${json}{${importedPersonForms[count.count - 1].firstname}:${perForm.firstname},"/>
    <c:set var="json" value="${json}${importedPersonForms[count.count - 1].lastname}:${perForm.lastname},"/>    

    <input type="hidden" name="${importedPersonForms[count.count - 1].firstname}" value="${perForm.firstname}"/>
    <input type="hidden" name="${importedPersonForms[count.count - 1].lastname}" value="${perForm.lastname}"/>

    <c:forEach var="position" items="${perForm.positions}">
        <c:set var="json" value="${json}${importedPersonForms[count.count - 1].positions}:${position},"/>   
        <input type="hidden" name="${importedPersonForms[count.count - 1].positions}" value="${position}"/>
    </c:forEach>
    <c:forEach var="organization" items="${perForm.organizations}">
        <c:set var="json" value="${json}${importedPersonForms[count.count - 1].organizations}:${organizations},"/>  
        <input type="hidden" name="${importedPersonForms[count.count - 1].organizations}" value="${organization}"/>
    </c:forEach>

    <c:set var="json" value="${json}addressList: [{"/>
    <c:set var="json" value="${json}${importedPersonForms[count.count - 1].addressList[0].countryCode}:${perForm.addressList[0].countryCode},"/>
    <c:set var="json" value="${json}${importedPersonForms[count.count - 1].addressList[0].number}:${perForm.addressList[0].number},"/>
    <c:set var="json" value="${json}${importedPersonForms[count.count - 1].addressList[0].fixedLine}:${perForm.addressList[0].fixedLine}"/>
    <c:set var="json" value="${json}}]"/>

    <%-- if current index is not last then append a comma --%>
    <c:if test="${count.index - 1 == fn:length(importedPersonForms)}">
        <c:set var="json" value="${json}},"/>
    </c:if>

    <input type="hidden" name="${importedPersonForms[count.count - 1].addressList[0].countryCode}" value="${perForm.addressList[0].countryCode}"/>
    <input type="hidden" name="${importedPersonForms[count.count - 1].addressList[0].number}" value="${perForm.addressList[0].number}"/>
    <input type="hidden" name="${importedPersonForms[count.count - 1].addressList[0].fixedLine}" value="${perForm.addressList[0].fixedLine}"/>
</c:forEach>
<c:set var="json" value="${json}]}"/>
<input type="input" name="inputJson" value="${json}"/>
于 2013-10-22T07:06:03.073 回答