Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

The writing method of json array

2025-01-17 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >

Share

Shulou(Shulou.com)06/02 Report--

Editor to share with you how to write the json array. I hope you will get something after reading this article. Let's discuss it together.

Json array is written: 1, use [for-in] to access the array; 2, use index values to modify array values; 3, use the delete keyword to delete array elements, the code is [delete myObj.sites [1];].

Json array is written as follows:

Array as JSON object

["Google", "Runoob", "Taobao"]

The JSON array is written in square brackets.

The array value in JSON must be a valid JSON data type (string, number, object, array, Boolean, or null).

In JavaScript, the array value can be the above JSON data type, or the expression of JavaScript, including function, date, and undefined.

Arrays in JSON objects

The value of an object property can be an array:

{"name": "website", "num": 3, "sites": ["Google", "Runoob", "Taobao"]}

We can use index values to access the array:

X = myObj.sites [0]

Cyclic array

You can use for-in to access the array:

For (I in myObj.sites) {x + = myObj.sites [I] + "

";}

You can also use the for loop:

For (I = 0; I < myObj.sites.length; iTunes +) {x + = myObj.sites [I] + "

";}

Arrays in nested JSON objects

An array in a JSON object can contain another array, or another JSON object:

MyObj = {"name": "website", "num": 3, "sites": [{"name": "Google", "info": ["Android", "Google search", "Google translation"]}, {"name": "Runoob", "info": ["rookie tutorial", "rookie tool", "rookie Wechat"]} {"name": "Taobao", "info": ["Taobao", "online shopping"}]}

We can use for-in to iterate through each array:

For (I in myObj.sites) {x + = "" + myObj.sites [I] .name + "; for (j in myObj.sites.info) {x + = myObj.sites.info [j] +"

";}}

Modify array values

You can use index values to modify array values:

MyObj.sites [1] = "Github"

Delete array elements

We can use the delete keyword to delete array elements:

Delete myObj.sites [1]; after reading this article, I believe you have a certain understanding of "how to write an json array". If you want to know more about it, please follow the industry information channel. Thank you for reading!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Internet Technology

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report