当前位置:网站首页>两步处理字符串正则匹配得到JSON列表
两步处理字符串正则匹配得到JSON列表
2022-06-26 05:02:00 【淡灰灰灰】
先说一下为什么要做这个事情
这几天遇见个比较毒的问题,elementplus的table表格不是要键值对对应的json数据列表来显示数据嘛。
我写的一个小项目用的后端是go语言,本意是后端在数据库得到所有数据以后,在后端处理好:将所有数据打包成一个带着所有json格式数据的列表。
再将这个列表传给前端,直接赋值列表就好了。结果go语言的列表不像python的列表是实际存在的,go语言的列表是实际的链表,我的前端不能得到我想要的json数据列表。
于是就想到了后端把带着所有json数据的字符串传给前端,由前端js处理字符串,给我想要的json列表。
再推荐一个正则表达式测试工具:正则表达式在线测试 | 菜鸟工具 (runoob.com)
接下来就是简单的步骤:
得到后端给的json数据
//得到后端给的json数据
var str = response['data']
正则匹配所有包括大括号的内容,返回一个数组
//正则匹配所有包括大括号的内容,返回一个数组
str = str.match(/{[^}{]*?}/g)
eval()定义:eval() 函数可计算某个字符串,并执行其中的的 JavaScript 代码。
由于 JSON 语法是 JavaScript 语法的子集,JavaScript 函数 eval() 可用于将 JSON 文本转换为 JavaScript 对象
现在列表里还都是字符串形式的json,遍历数组将字符串转换为json
//但是现在列表里还都是字符串形式的json,遍历数组将字符串转换为json
for(var i=0;i<str.length;i++){
//eval()定义:eval() 函数可计算某个字符串,并执行其中的的 JavaScript 代码。
//由于 JSON 语法是 JavaScript 语法的子集,JavaScript 函数 eval() 可用于将 JSON 文本转换为 JavaScript 对象
str[i] = eval ("(" + str[i] + ")")
//控制台检查数据
console.log(str[i])
}边栏推荐
- torchvision_ Transform (image enhancement)
- Computer Vision Tools Chain
- torchvision_transform(图像增强)
- Essential foundation of programming - Summary of written interview examination sites - computer network (1) overview
- Hash problem
- Créateur de génie: cavalier solitaire, magnat de la technologie et ai | dix ans d'apprentissage profond
- Multipass Chinese document - setup driver
- Rdkit chemical formula molecular formula search
- UWB超高精度定位系统原理图
- date_ Range creation date range freq parameter value table and creation example
猜你喜欢

超高精度定位系统中的UWB是什么

Multipass中文文档-设置驱动

UWB超高精度定位系统原理图

86.(cesium篇)cesium叠加面接收阴影效果(gltf模型)

LeetCode 19. Delete the penultimate node of the linked list

ModuleNotFoundError: No module named ‘numpy‘

天才制造者:獨行俠、科技巨頭和AI|深度學習崛起十年

钟珊珊:被爆锤后的工程师会起飞|OneFlow U

1.12 learning summary

Genius makers: lone Rangers, technology giants and AI | ten years of the rise of in-depth learning
随机推荐
微服务之间的Token传递之一@Feign的token传递
Hash problem
5. < tag stack and general problems > supplement: lt.946 Verify the stack sequence (the same as the push in and pop-up sequence of offer 31. stack)
Comment enregistrer une image dans une applet Wechat
Multipass Chinese document - remote use of multipass
A ZABBIX self discovery script (shell Basics)
Multipass中文文档-移除实例
Numpy data input / output
DBeaver 安装及配置离线驱动
Dbeaver installation and configuration of offline driver
Day3 data type and Operator jobs
Numpy index and slice
Multipass Chinese document - use packer to package multipass image
Genius makers: lone Rangers, technology giants and AI | ten years of the rise of in-depth learning
Schematic diagram of UWB ultra high precision positioning system
MySql如何删除所有多余的重复数据
A company crawling out of its grave
Cookie and session Basics
Introduction to classification data cotegory and properties and methods of common APIs
Astype conversion data type