简体   繁体   English

使用Bean对象中的JSON构造数组

[英]Construct array using JSON from Bean object

Hello all I have a bean that has 3 getters. 大家好,我有一个具有3个吸气剂的bean。 In the JSP I use JSTL to iterate over the bean to populate a table. 在JSP中,我使用JSTL遍历bean以填充表。 I have saome javascript I need to do the same thing to construct an array. 我有一些JavaScript,我需要做同样的事情来构造一个数组。 Here it is hardcoded, but how can I contruct it by itearing over a bean? 在这里它是硬编码的,但是如何通过在bean上安装它来构造它呢?

Bean: This is how I do it in the JSP using JSTL Bean:这就是我在JSP中使用JSTL的方法

<c:forEach var="bean" items="${beans}">
     ${bean.month}       
     </c:forEach>

How Can I do the same thing here: 我如何在这里做同样的事情:

Javascript: Javascript:

"categories": [{
    "category": [{
       "label": "Oct"
    }, {
        "label": "Nov"
    }, {
        "label": "Dec"
    }, {
       "label": "Jan"
    }, {
        "label": "Feb"
    }, {
        "label": "Mar"
    }, {
        "label": "Apr"
    }, {
        "label": "May"
    }, {
        "label": "Jun"
    }, {
        "label": "Jul"
    }, {
        "label": "Aug"
    }, {
        "label": "Sep"
    }]     
    }]

Trying to do something like this in javascript 试图在javascript中做这样的事情

 <c:forEach var="bean" items="${beans}">
     [{
       "label": " ${bean.month}"
    },         
     </c:forEach>

I am not well experienced in JSTL. 我在JSTL方面经验不足。 This is a guess based on the experience I have in PHP. 这是根据我在PHP中的经验得出的猜测。

var array = [
<c:forEach var="bean" items="${beans}" varStatus="beanStatus">
    {
        "label": "${bean.month}"
    }
    <c:if test="${!beanStatus.last}">  // put comma after all item, but last one
         ,
    </c:if>          
 </c:forEach>
];

or 要么

var array = [];
<c:forEach var="bean" items="${beans}">
    array.push({
        "label": "${bean.month}"
    });         
</c:forEach>
var category = [], // new Array
    i,
    newCategory;

for (i = 0; i < beans.length; i += 1) {
    newCategory = {}; // new object
    newCategory.label = beans[i].month;
    category.push(newCategory);
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM