当前位置:网站首页>JSON value acquisition and traversal [json]
JSON value acquisition and traversal [json]
2022-07-21 18:35:00 【Java code evolution ing】
JSON Value acquisition and traversal
obtain JSON data
- JSON object . Key name
- JSON object [“ Key name ”]
- Be careful : When using this method, the key name should be ""( Double quotes ) Include , It's in string form
- Array objects [ Indexes ]
- If JSON Object is in array , Then use this method
For example, to understand :
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>JSON Value acquisition </title>
<script> //1. Define the basic format var person = {
"name":" Zhang San ","age":23,"gender":true}; var name = person.name; alert(name); var name = person["name"]; alert(name); //2. Nesting format : {} -> [] , This means JSON Object [] Array var persons = {
"persons" : [ {
"name":" Zhang San ","age":23,"gender":true}, {
"name":" Li Si ","age":23,"gender":true} ] } // This way is to use the key to get the value first , But after getting the value, the value is an array , There are objects in the array , So we get the array // After a certain value in, you can still use the corresponding key to obtain the value var name1 = persons.persons[1].name; alert(name1); //3. Nesting format : [] -> {} , Here, it means that the array is nested JSON object var ps = [ {
"name":" Li Si ","age":23,"gender":true}, {
"name":" Li Si ","age":23,"gender":true}, {
"name":" Li Si ","age":23,"gender":true}, {
"name":" Li Si ","age":23,"gender":true} ] // In this way, we directly locate an object through the index of the array , Then get the corresponding value through the key var name2 = ps[1].name; alert(name2); </script>
</head>
<body>
</body>
</html>
JSON Data traversal :
Use for in loop :
For example, to understand :
var person = {
"name":" Zhang San ","age":23,"gender":true};
for (let key in person) {
alert(key);
}
Be careful : Here is the will person The keys in the object are all traversed , Each iteration will temporarily store the key of the object to a temporary variable key in
If it is JSON Object in array , Then we add an outer layer when traversing for Circulation is enough :( as follows :)
//3. Nesting format : [] -> {} , Here, it means that the array is nested JSON object
var ps = [
{
"name":" Zhang San ","age":23,"gender":true},
{
"name":" Li Si ","age":23,"gender":true},
{
"name":" Wang Wu ","age":23,"gender":true},
{
"name":" Li Si ","age":23,"gender":true}
]
for (var i = 0; i < ps.length; i++) {
var p = ps[i];
for (let key in p) {
alert(key + ":" + p[key])
}
}
- This method is actually the same as the previous one , Actually traversal JSON Objects are always used for let in loop , Just because the elements in the array are one by one in this traversal process JSON object , So we need to use a common for Loop through the array
Add :
for let in Loop traversal JSON Data is like us Java Strengthening in for loop (foreach) equally , Each time the traversed data is stored in a temporary variable , It's just different for let in Loop traversal JSON Array is traversed to the key every time , So it's what you will traverse every time JSON The value of the key in the data is stored in a temporary variable
边栏推荐
- EasyNVS定制项目中的播放器更新及相应新功能增加
- JSON overview
- Ctfshow getting started with the web (phpcve)
- [wechat applet] component use and attribute reference
- 丹磺酰荧光素标记肽核酸偶联多肽|Dansyl-Ahx-PNA荧光素标记肽核酸的合成路线
- c语言---24 三子棋
- TiFlash 源码阅读(五) DeltaTree 存储引擎设计及实现分析 - Part 2
- pytorch-California House Prices
- Metal organic framework mil-101 (CR) loaded chitosan material | mil-101 (CR) @cs | glycyrrhetinic acid (GA) modified metal organic framework material uio-66-nh2 (uio-66- NH2 GA)
- 二维数组专题
猜你喜欢
JNDI注入之略微学学
GDB usage details
Ctfshow web entry command execution
[C] Language dynamic memory management
FeCl3 modified mil-101 (CR) ferric chloride | zirconium metal organic framework compound (uio-68-sazide) | cobalt based metal organic framework (zif-67) | MOF custom material
2D array theme
pytorch-California House Prices
Ctfshow getting started with the web (phpcve)
【IoT毕设.3】STM32单片机+机智云AIoT+猪舍监测与系统硬件设计
微软关闭基本身份验证,对企业与员工有什么影响?
随机推荐
Getting started with ctfshow web (PHP deserialization)
解决CNN固有缺陷, CCNN凭借单一架构,实现多项SOTA
注册表介绍及其维护
Okaleido tiger NFT即将登录Binance NFT平台,后市持续看好
[wechat applet] component use and attribute reference
dataframe 绘制相关系数拟合线 散点图拟合线
ant.design(简称antd)中时间DatePicker中RangePicker的日期选择限定为只能选择某一天的0点之前的日期
Web3 traffic aggregation platform starfish OS gives players a new paradigm experience of metauniverse
Registry introduction and maintenance
Can the content of the QR code be modified after the QR code is made?
Database table design (II): index specification and SQL statement specification
EasyNVS定制项目中的播放器更新及相应新功能增加
MFC calculator
Experimental requirements of cy5-pna cyanine dye Cy5 labeling PNA
羧甲基荧光素6-FAM修饰PNA肽核酸6-FAM-PNA|CY3-PNA荧光染料CY3偶联PNA肽核酸的应用
Pattern code lock
国内疫情反反复复,线下实体店的如何转型才能突破困境?
ant. The date selection of rangepicker in time datepicker in design (antd for short) is limited to the date before 0 o'clock of a certain day
2D array theme
齐岳mof|CdS [email protected]|甲基丙烯酸磺基甜菜碱(pSBMA)接枝改性UiO-66-PEI|活性氧化铝负载FeMOF绿色复合粒料