通过JSONArray 生成2层节点的tree

本文介绍了一种使用Java处理JSON数据的方法,通过遍历查询结果并构建JSON结构来组织数据。该过程涉及多个步骤,包括初始化必要的数据结构,如JSONArray、HashMap等,并根据特定条件向这些结构中添加元素。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

long str = 0;
JSONArray jsonArray = new JSONArray();
Map mapArray = new HashMap();
List list = new ArrayList();
List result = resultResource.getLacs();
Iterator o = result.iterator();
while (o.hasNext())
{
OcCsmCell ocCsmCell = (OcCsmCell) o.next();
Map mapObj = new HashMap();
if (ocCsmCell.getLai() == str)
{
mapObj.put("id", ocCsmCell.getLai());
mapObj.put("text", ocCsmCell.getLai());
mapObj.put("leaf", true);
mapObj.put("checked", false);
list.add(mapObj);
}
else
{
if (list.size() > 0)
{
mapArray.put("children", list);
jsonArray.add(mapArray);
}
list = new ArrayList();
mapArray = new HashMap();
str = ocCsmCell.getLai();
mapArray.put("id", ocCsmCell.getCi());
mapArray.put("text", ocCsmCell.getCi());
mapArray.put("cls", "folder");
mapArray.put("checked", false);
Map mapTemp = new HashMap();
mapTemp.put("id", ocCsmCell.getCi());
mapTemp.put("text", ocCsmCell.getCi());
mapTemp.put("leaf", true);
mapTemp.put("checked", false);
list.add(mapTemp);
}
if (!o.hasNext())
{
mapArray.put("children", list);
jsonArray.add(mapArray);
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值