将这些隐藏输入的值组合成一个单独的隐藏输入中的json格式化字符串

时间:2013-10-22 05:32:32

标签: javascript jquery json

我想将这些隐藏输入的值组合成一个单独的隐藏输入中的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>

1 个答案:

答案 0 :(得分: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}"/>