js processes JSON format data

Insert image description here
It is very likely that you will encounter json problems during project development. It may be that the backend needs the frontend to process and escape parameters to the backend, or it may be that the frontend needs to process it by itself for display. We can solve it with a simple example
.

let arr = [1,2,3, { name : 'jack'} ];  // 假设后端返回的是这样的数组类型,前端去处理,那么用 JSON.stringify() 方法即可。
let jsonarr = JSON.stringify(arr); 
console.log(jsonarr)  // [1,2,3,{"name":"jack"}]  数字变更成了字符串类型
// 如果需要转成数组 ,我们在用 JSON.parse() 方法进行处理即可
let parr = JSON.parse(jsonarr);
console.log(parr) // [1, 2, 3, { name : 'jack'} ]

Nothing can change without departing from its roots.

Guess you like

Origin blog.csdn.net/lzfengquan/article/details/131122716