当前位置:网站首页>复杂 json数据 js前台解析 详细步骤《案例:一》
复杂 json数据 js前台解析 详细步骤《案例:一》
2022-07-02 05:59:00 【一轮大月亮】
一、直接上代码 !
这是后台传到前台的json格式字符串:
var p = '{"spec": [{"a": "XS"}, {"b": "S"}, {"2": "M"}, {"3": "L"}, {"4": "XL"}, {"5": "XXL"}]}'
1.第一步先转 先将json字符串转为JavaScript对象:
var a =JSON.parse(p) ;
直接输出,看一下数据格式
a
验证一下数据格式
typeof(a)
typeof(p)
验证没问题!
2.循环数据
如下图所示,分析数据格式:
JavaScript对象 属性名称:spec 对应值为长度为6的数组,
6个元素 分别有自己的key和对应的value
3.循环数组,拿数据!
拿到数组:
a.spec
a.spec.forEach(function(obj,index){
console.log(Object.keys(obj)[0]);
console.log(Object.values(obj)[0]);
})
通过 Object.keys(obj)[0] 方法,获取 数组 元素对象的key值,循环并打印:
通过 Object.values(obj)[0] 方法,获取 数组 元素对象的value值,循环并打印:
好了,就到这里了,有疑问,
欢迎交流:QQ 1531613221 QQ群 201455739
边栏推荐
- 在uni-app中引入uView
- PHP array to XML
- 神机百炼3.54-染色法判定二分图
- 5g market trend in 2020
- PHP parent
- 【论文翻译】GCNet: Non-local Networks Meet Squeeze-Excitation Networks and Beyond
- 数据挖掘方向研究生常用网站
- cookie插件和localForage离线储存插件
- Stc8h8k Series Assembly and c51 Real combat - NIXIE TUBE displays ADC, Key Series port reply Key number and ADC value
- Conglin environmental protection rushes to the scientific and Technological Innovation Board: it plans to raise 2billion yuan, with an annual profit of more than 200million yuan
猜你喜欢
vite如何兼容低版本浏览器
OLED12864 液晶屏
[whether PHP has soap extensions installed] a common problem for PHP to implement soap proxy: how to handle class' SoapClient 'not found in PHP
3D printer G code command: complete list and tutorial
Brain and cognitive neuroscience matlab psychoolbox cognitive science experimental design - experimental design 4
Keepalived installation, use and quick start
Stick to the big screen UI, finereport development diary
经典文献阅读之--SuMa++
Vscode paste image plugin saves image path settings
Software testing Q & A
随机推荐
492.构造矩形
PHP gets CPU usage, hard disk usage, and memory usage
Software testing - concept
图片裁剪插件cropper.js
Reading notes of cgnf: conditional graph neural fields
Grbl software: basic knowledge of simple explanation
Brain and cognitive neuroscience matlab psychoolbox cognitive science experimental design - experimental design 4
Summary of MySQL constraints
PHP read file (read the specified line containing a string in the file)
Scheme and implementation of automatic renewal of token expiration
vite如何兼容低版本浏览器
Oled12864 LCD screen
LCD之MIPI协议的一些说明
JWT工具类
在uni-app中引入uView
Huawei Hongmeng OS, is it OK?
php父类(parent)
Happy Lantern Festival | Qiming cloud invites you to guess lantern riddles
Alibaba: open source and self-developed liquid cooling data center technology
3D printer G code command: complete list and tutorial