search
HomeCommon ProblemHow to write json array

How to write json array

Feb 09, 2021 am 10:17 AM
json array

How to write json array: 1. Use [for-in] to access the array; 2. Use the index value to modify the array value; 3. Use the delete keyword to delete the array element, the code is [delete myObj. sites[1];].

How to write json array

#The operating environment of this article: Windows 7 system, Dell G3 computer.

How to write json array:

Array as JSON object

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

JSON array in Written in brackets.

Array values ​​in JSON must be legal JSON data types (string, number, object, array, Boolean or null).

In JavaScript, array values ​​can be the above JSON data types, or JavaScript expressions, including functions, dates, and undefined.

Array in JSON object

The value of the object property can be an array:

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

We can use the index value to access the array:

x = myObj.sites[0];

Looping through an array

You can use for-in to access an array:

for (i in myObj.sites) 
{ x += myObj.sites[i] + "<br>"; }

You can also use a for loop:

for (i = 0; i < myObj.sites.length; i++) 
{ x += myObj.sites[i] + "<br>"; }

Arrays in nested JSON objects

The array in the JSON object can contain another array, or another JSON object:

myObj = {
    "name":"网站",
    "num":3,
    "sites": [
        { "name":"Google", "info":[ "Android", "Google 搜索", "Google 翻译" ] },
        { "name":"Runoob", "info":[ "菜鸟教程", "菜鸟工具", "菜鸟微信" ] },
        { "name":"Taobao", "info":[ "淘宝", "网购" ] }
    ]
}

We can use for-in to loop Access each array:

for (i in myObj.sites) {
    x += "<h1 id="nbsp-nbsp-myObj-sites-i-name-nbsp-nbsp">" + myObj.sites[i].name + "</h1>";
    for (j in myObj.sites[i].info) {
        x += myObj.sites[i].info[j] + "<br>";
    }
}

Modify the array value

You can use the index value to modify the array value:

myObj.sites[1] = "Github";

Delete the array element

We can use the delete keyword to delete array elements:

delete myObj.sites[1];

Related free learning recommendations: php programming(video)

The above is the detailed content of How to write json array. For more information, please follow other related articles on the PHP Chinese website!

Statement
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Tools

Atom editor mac version download

Atom editor mac version download

The most popular open source editor

VSCode Windows 64-bit Download

VSCode Windows 64-bit Download

A free and powerful IDE editor launched by Microsoft

WebStorm Mac version

WebStorm Mac version

Useful JavaScript development tools

MantisBT

MantisBT

Mantis is an easy-to-deploy web-based defect tracking tool designed to aid in product defect tracking. It requires PHP, MySQL and a web server. Check out our demo and hosting services.

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment