使用 fast Json 对遍历数据库中的文本字段进行遍历并排序和是否启用

使用 fast Json 对遍历数据库中的文本字段进行遍历并排序和是否启用

最近 项目中第一次写到了对 数据库中的文本字段保存的json 格式进行遍历 ,使用 fast json解决了这问题记录下

数据库保存的这个这样的数据:

{
    
    
    "projectId":"1027",
    "companyId":"1026",
    "isEnabled":1,
    "position":2,
    "list":[
        {
    
    
            "type":1,
            "name":"微微林",
            "icon":"https://static.laoganbu.cn/20200925/340f70da76da4af7b3546be5ea29ad1d.png",
            "phone":"13240555624",
            "orderNum":"1",
            "isEnable":1
        },
        {
    
    
            "type":2,
            "name":"唯一好",
            "icon":"https://static.laoganbu.cn/20200925/61bd569279ea4c779ec98e6e1baf0bb2.jpeg",
            "guide":"开心就好",
            "orderNum":"4",
            "isEnable":1
        },
        {
    
    
            "type":3,
            "name":"深圳活动报名",
       "icon":"https://static.laoganbu.cn/20200925/43d292e2e52a4db09ec904ba6a8ef11f.jpeg",
            "path":"pages/index/index.html",
            "appId":"wx98cce962e5ac41a5",
            "orderNum":"3",
            "isEnable":1
        }
    ]
}

遍历的过程中保存到实体类中:

package cn.ibona.modules.sys.entity;

import lombok.Data;

/**
 * 悬浮按钮实体类
 * @ProjectName: hhds-base-b-server
 * @Package: cn.ibona.modules.sys.entity
 * @ClassName: SuspendButtonEntity
 * @Author: wym
 * @Date: 2020/9/23 16:00
 * @Version: 1.0
 */
@Data
public class SuspendButtonEntity {
    
    
        /**
         * 悬浮类型
         */
        private Integer type;
        /**
         * 名称
         */
        private  String name;
        /**
         * 图标
         */
        private String icon;
        /**
         * 电话
         */
        private String phone;
        /**
         * 排序
         */
        private Integer orderNum;
        /**
         * 是否启用
         */
        private Integer isEnable;
        /**
         * appid
         */
        private String appId;
        /**
         * 指南
         */
        private String guide;
        /**
         * 公众号图片
         */
        private String wxIcon;
}

@GetMapping("/suspendButton")
	public R getSuspendButton(@RequestParam Map<String,Object> params){
    
    
        //根据前端传入的参数进行查询到这个配置中信息 
		SysConfigEntity sysConfigEntity = sysConfigService.querySuspendButton(params);
		if(sysConfigEntity != null){
    
    
			//字符串转json
			JSONObject buffonInfo = JSON.parseObject(sysConfigEntity.getParamValue());
			Integer isEnabled = buffonInfo.getInteger("isEnabled");
			Integer position = buffonInfo.getInteger("position");
            //
			JSONArray list = buffonInfo.getJSONArray("list");
			List<SuspendButtonEntity>  suspendButtonEntityS= new ArrayList<>();
			for (int i = 0; i < list.size(); i++) {
    
    
                //将其保存的对应的实体类中
				SuspendButtonEntity suspendButtonEntity = 
                    JSON.toJavaObject(list.getJSONObject(i), SuspendButtonEntity.class);
				if ((Constant.System.NOT_ENABLED.getValue())!= Public.mapTo(suspendButtonEntity.getIsEnable(), 1)){
    
    
					suspendButtonEntityS.add(suspendButtonEntity);
				}
			}
			//根据排序字段排序
			suspendButtonEntityS.sort(Comparator.comparing(SuspendButtonEntity::getOrderNum,Comparator.nullsFirst(Comparator.naturalOrder())));
			Map<Object, Object> data = new HashMap<>(3);
			data.put("position",position);
			data.put("isEnabled",isEnabled);
			data.put("list",suspendButtonEntityS);
			return R.ok().put("data",data);
		}
		return  R.ok().put("data",null);
	}

猜你喜欢

转载自blog.csdn.net/weng74/article/details/108863823
今日推荐