Home Common Problem How 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" ]
Copy after login

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" ] 
}
Copy after login

We can use the index value to access the array:

x = myObj.sites[0];
Copy after login

Looping through an array

You can use for-in to access an array:

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

You can also use a for loop:

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

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":[ "淘宝", "网购" ] }
    ]
}
Copy after login

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

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

Modify the array value

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

myObj.sites[1] = "Github";
Copy after login

Delete the array element

We can use the delete keyword to delete array elements:

delete myObj.sites[1];
Copy after login

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 of this Website
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

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
4 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. Best Graphic Settings
4 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. How to Fix Audio if You Can't Hear Anyone
4 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
WWE 2K25: How To Unlock Everything In MyRise
1 months ago By 尊渡假赌尊渡假赌尊渡假赌

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

Detailed explanation of sorting and filtering operations of JSON arrays in Java. Detailed explanation of sorting and filtering operations of JSON arrays in Java. Sep 06, 2023 pm 03:22 PM

Detailed explanation of sorting and filtering operations of JSON arrays in Java In Java development, processing JSON data is a common task. As one of the commonly used data structures, JSON array often requires sorting and filtering operations in practical applications. This article will introduce in detail the sorting and filtering operations of JSON arrays in Java and provide corresponding code examples. 1. Sorting operation of JSON array: Use JSONArray object to store JSON array in Java, and use json library to process JSON data

Get started quickly: JSON array merging and splitting techniques in Java. Get started quickly: JSON array merging and splitting techniques in Java. Sep 06, 2023 am 10:21 AM

Get started quickly: JSON array merging and splitting techniques in Java In modern software development, data format and transmission have become increasingly important. Among them, JSON (JavaScriptObjectNotation) is a commonly used data format, especially suitable for front-end and back-end interaction and data storage. In Java development, we often need to deal with JSON objects and JSON arrays. This article explains how to merge and split JSON arrays in Java, along with tips and examples for implementing these operations.

How to convert JSON array to CSV in Java? How to convert JSON array to CSV in Java? Aug 21, 2023 pm 08:27 PM

JSON can be used as a data exchange format, it is lightweight and language independent. A JSONArray can parse text strings to produce vector-like objects and supports the java.util.List interface. We can convert JSON array to CSV format using org.json.CDL class, which provides a static method toString() for converting JSONArray to comma-separated text. We need to import the org.apache.commons.io.FileUtils package to store data in a CSV file using the writeStringToFile() method. Syntaxpublicstaticj

How can we merge two JSON arrays in Java? How can we merge two JSON arrays in Java? Aug 20, 2023 pm 11:05 PM

A JSON is a lightweight data exchange format, and the format of JSON is a key-value pair. JSONArray can parse text from a string to generate a vector-like object and supports the java.util.List interface. We can merge two JSON arrays in Java using org.json.simple.JSONArray class. We can merge two JSON arrays in the following program using the addAll() method (inherited from interface java.util.List). Example

Beginner's Guide: FAQs on Manipulating JSON Arrays in Java. Beginner's Guide: FAQs on Manipulating JSON Arrays in Java. Sep 06, 2023 am 11:22 AM

Beginner's Guide: FAQs on Manipulating JSON Arrays in Java Summary: With the development of the Internet, JSON (JavaScriptObjectNotation) has become a common format for data exchange. In Java development, manipulating JSON arrays is a common task. This article will answer common questions about operating JSON arrays in Java development and provide code examples. How to create a JSON array? In Java, you can use third-party libraries such as JSON-java

How to deserialize JSON array to generic type of list in Java? How to deserialize JSON array to generic type of list in Java? Aug 20, 2023 pm 12:13 PM

The Gson library provides a class named com.google.gson.reflect.TypeToken to store generic types by creating a GsonTypeToken class and passing the class type. Using this type, Gson can know the class passed in the generic class. Syntax publicclassTypeToken&lt;T&gt;extendsObject We can deserialize a JSON array into a list of generic types in the following example importjava.lang.reflect.Type;importjava.util.*;importcom.go

How to parse and traverse JSON array in JAVA? Master JSON array processing skills. How to parse and traverse JSON array in JAVA? Master JSON array processing skills. Sep 06, 2023 am 11:30 AM

How to parse and traverse JSON array in JAVA? Master JSON array processing skills. With the rapid development of the modern Internet, JSON (JavaScriptObjectNotation) has become a commonly used data exchange format. It is concise, easy to read, and very suitable for data transmission in web development and API interfaces. In JAVA, parsing and traversing JSON arrays are very common operations. This article will introduce how to use JAVA to parse JSON arrays and give the corresponding

How can we implement JSON array using streaming API in Java? How can we implement JSON array using streaming API in Java? Sep 19, 2023 pm 06:01 PM

The JsonGenerator interface can be used to stream JSON data to an output source. We can create or implement a JSON array using JsonGenerator's writeStartArray() method, which writes a JSON name/start array character pair in the current object context. The writeStartObject() method writes the JSON starting object character, which is only valid in array context, and the writeEnd() method writes the end of the current context. Syntax JsonGeneratorwriteStartArray(Stringname) example importjava.io.*;imp