jstl foreach 怎么遍历json 数组

jstl foreach 怎么遍历json 数组,第1张

<c:forEach items="${jsonArray }" var="bean">

${beanXXX }

</c:forEach>

<%

//简单的map对象

Map map = new HashMap();

mapput("aa", "你好");

mapput("bb","中国");

requestsetAttribute("person1", map);  

//把map放入request域对象,真正的时候是有另一个控制器传送过来的,在这里我直接放在同一个页面,下面也一样 

//map中放入User对象

User u1 = new User();

u1setName("hello");

u1setAge(18);

User u2 = new User();

u2setName("world");

u2setAge(21);

mapput("aa", u1);

mapput("bb",u2);

requestsetAttribute("person", map);

//set

Set set = new HashSet();

setadd(u1);

setadd(u2);

requestsetAttribute("person2", set);

%>

<!-- 遍历简单的map对象

<c:forEach items="${person1 }" var="per1">

key=${per1key },value=${per1value }

</c:forEach><br>

-->

<!-- 遍历Map中的User对象 -->

<h2>map的迭代</h2>

<c:forEach items="${person}" var="user">

key=${userkey },name=${uservaluename },age=${ uservalueage}

</c:forEach><br>

<h2>Set集合的遍历</h2>

<c:forEach items="${person2 }" var="per">

name=${pername },age=${perage }

</c:forEach> 

</body>

上述的User中有两个属性,分别是name和age,以及相对应的set和get的方法。

1、创建python文件,testjsonpy;

2、编写python代码,解析json数组

json_str = {"name":"lisi",

"age":27}

for key, value in json_stritems():

print(str(key) + '=' + str(value))

3、右击,选择‘在终端中运行Python文件’;

4、查看运行结果,即可发现解析完成;

var origin = [

{"first_id":1,"first_name":"中学","second_id":"1-1","second_name":"一年级","third_id":"1-1-1","third_name":"一年级一班","people":10,"age":10,"parent":5},

{"first_id":1,"first_name":"中学","second_id":"1-1","second_name":"一年级","third_id":"1-1-2","third_name":"一年级二班","people":11,"age":10,"parent":5},

{"first_id":1,"first_name":"中学","second_id":"1-2","second_name":"二年级","third_id":"1-2-1","third_name":"二年级一班","people":20,"age":10,"parent":5},

{"first_id":1,"first_name":"中学","second_id":"1-2","second_name":"二年级","third_id":"1-2-2","third_name":"二年级二班","people":21,"age":10,"parent":5},

{"first_id":2,"first_name":"高中","second_id":"2-1","second_name":"一年级","third_id":"2-1-1","third_name":"一年级一班","people":31,"age":10,"parent":5}

];

var finalData = []; // 最终的数据

transferData(); // 数据转换

consolelog(finalData, JSONstringify(finalData));

function transferData() {

originforEach(function(n) {

var first = getRecordById(nfirst_id, finalData);

if (first) {

firstage += nage;

firstparent += nparent;

firstpeople += npeople;

var second = getRecordById(nsecond_id, firstchildren);

if (second) {

secondage += nage;

secondparent += nparent;

secondpeople += npeople;

var third = getRecordById(nthird_id, secondchildren);

if (third) {

// 这里应该不会存在

} else {

secondchildrenpush({

id: nthird_id,

name: nthird_name,

age: nage,

parent: nparent,

people: npeople

});

}

} else {

firstchildrenpush({

id: nsecond_id,

name: nsecond_name,

age: nage,

parent: nparent,

people: npeople,

children: [{

id: nthird_id,

name: nthird_name,

age: nage,

parent: nparent,

people: npeople

}]

});

}

} else {

finalDatapush({

id: nfirst_id,

name: nfirst_name,

age: nage,

parent: nparent,

people: npeople,

children: [{

id: nsecond_id,

name: nsecond_name,

age: nage,

parent: nparent,

people: npeople,

children: [{

id: nthird_id,

name: nthird_name,

age: nage,

parent: nparent,

people: npeople

}]

}]

});

}

});

}

function getRecordById(id, data) {

for (var i = 0, n = datalength; i < n; i++) {

if (data[i]id == id) return data[i];

}

return null;

}

新new一个数组array;

在for循环中将每个json放入这个新new的数组array;

在for循环的外部获取这个新new的数组;

以上就是关于jstl foreach 怎么遍历json 数组全部的内容,包括:jstl foreach 怎么遍历json 数组、python中如何遍历json数组、跪求大神,用js或者java循环遍历json数组,实现下面功能,太难了,实在不会,跪求了(6)。等相关内容解答,如果想了解更多相关内容,可以关注我们,你们的支持是我们更新的动力!

欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/sjk/9880211.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2023-05-02
下一篇 2023-05-02

发表评论

登录后才能评论

评论列表(0条)

保存