赞
踩
- const jsonObj = {
- "name": "牙膏",
- age: 45,
- "count": 10,
- "orderDetail": 32143214214,
- "orderId": 78909890,
- "more": {
- "desc": "描述"
- }
- }
- // 筛选数据
- console.log(JSON.stringify(jsonObj, ['name', 'age']))//{"name":"牙膏","age":45}
- const jsonString = JSON.stringify(jsonObj, function (key, value) {
- if (typeof value === 'string') {
- return undefined
- }
- return value
- })
- //{"age":45,"count":10,"orderDetail":32143214214,"orderId":78909890,"more":{}}
- console.log(jsonString)
- /**
- * {
- "name": "牙膏",
- "age": 45,
- "count": 10,
- "orderDetail": 32143214214,
- "orderId": 78909890,
- "more": {
- "desc": "描述"
- }
- }
- */
- // 格式化数据
- console.log(JSON.stringify(jsonObj, null, '\t'))
注意: 如果需求中有要求将json字符串显示为容易阅读的形式时应使用: JSON.stringify(对象变量xxobject, null, '\t')
- let product = {
- "name": "牙膏",
- age: 45,
- "count": 10,
- "orderDetail": 32143214214,
- "orderId": 78909890,
- "more": {
- "desc": "描述"
- },
- toJSON() {
- return {
- name: '哇哈哈'
- }
- }
- }
- /** 打印结果:
- * {
- "name": "哇哈哈"
- }
- */
- console.log(JSON.stringify(product, null, '\t'))
- let jsonStr = `{
- "name": "牙膏",
- "count": 10,
- "orderDetail": 32143214214,
- "orderId": 78909890,
- "more": {
- "desc": "描述"
- }
- }`
-
- const obj = JSON.parse(jsonStr, function (k, value) {
- console.log('key:', k, 'this:', this, 'value:', value)
- if (typeof value === 'string') {
- return undefined
- }
- return value
- })
- // {"count":10,"orderDetail":32143214214,"orderId":78909890,"more":{}}
- console.log(JSON.stringify(obj))
赞
踩
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。