JS:前端多层Json取值案例

先看Json字符串:

[{“Id”:“3ebba711-43b4-4d9a-b5ab-078e7bed72d5”,“Title”:“测试”,“Content”:“刘某”,“rectFile”:[{“RectId”:“3ebba711-43b4-4d9a-b5ab-078e7bed72d5”,“MiddleId”:“2910819b-3210-4e24-84f2-6bfdcb081f9b”,“FileId”:“a159d2a6-d718-4a29-a35b-8d16857fc380”,“FileName”:“教育督导系统功能清单2021.1.30.xlsx”},{“RectId”:“3ebba711-43b4-4d9a-b5ab-078e7bed72d5”,“MiddleId”:“7dcd0d1e-e901-46d5-aa60-3a5980e2731e”,“FileId”:“76e41682-072f-4bfd-a624-7103472a749a”,“FileName”:“rb_pgx2数据库文档说明180622.docx”}],“CreateTime”:“2021-11-23 11:30:29”,“FileName”:“教育督导系统功能清单2021.1.30.xlsx”}]

BeJson格式化校验一下:

[{
	"Id": "3ebba711-43b4-4d9a-b5ab-078e7bed72d5",
	"Title": "测试",
	"Content": "刘某",
	"rectFile": [{
		"RectId": "3ebba711-43b4-4d9a-b5ab-078e7bed72d5",
		"MiddleId": "2910819b-3210-4e24-84f2-6bfdcb081f9b",
		"FileId": "a159d2a6-d718-4a29-a35b-8d16857fc380",
		"FileName": "教育督导系统功能清单2021.1.30.xlsx"
	}, {
		"RectId": "3ebba711-43b4-4d9a-b5ab-078e7bed72d5",
		"MiddleId": "7dcd0d1e-e901-46d5-aa60-3a5980e2731e",
		"FileId": "76e41682-072f-4bfd-a624-7103472a749a",
		"FileName": "rb_pgx2数据库文档说明180622.docx"
	}],
	"CreateTime": "2021-11-23 11:30:29",
	"FileName": "教育督导系统功能清单2021.1.30.xlsx"
}]

现在要取rectFile中的FileName, 定义一个Jsonresc,序列化接收该字符串,然后前端用for循环,后台可以用foreach, 这样:

  let jsonresc = JSON.parse(resc);
            let con = "";
            //最外层
            for (var item in jsonresc)
            {
            //取rectFile
                for (var index in jsonresc[item].rectFile) {
                    if ((jsonresc[item].rectFile[index].FileId != null || jsonresc[item].rectFile[index].FileId != "") && (jsonresc[item].rectFile[index].FileName != null || jsonresc[item].rectFile[index].FileName!=""))
                    {
                    //取值
                        $("#rectFile").append(` <a class="download" type="text" lay-event="download"  data-fileid=${jsonresc[item].rectFile[index].FileId}>
                    <span class="layui-inline"> ${jsonresc[item].rectFile[index].FileName == null ? "" : jsonresc[item].rectFile[index].FileName}</span>&nbsp;&nbsp</a>
                    
                    <a lay-event="Delete" class="Delete" data-fileid=${jsonresc[item].rectFile[index].FileId == null ? "" : jsonresc[item].rectFile[index].FileId}>${jsonresc[item].rectFile[index].FileId == null ? "" : "删除"}</a><br/>`);
                    } 
                }
  • 0
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值