Get the App
SLTechnology News&Howtos  ›  Internet Technology  › 

The writing method of json array

Shulou Source: shulou.com Published: 2022-06-02 14:07:04 11月15日 Update

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!

Tags: Array object writing element index rookie loop key keyword data article type website legal code function character string finished attribute Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno MariaDB Apple Docker NVidia macOS