Get the App
SLTechnology News&Howtos  ›  Development  › 

How to understand JavaScript json object

Shulou Source: shulou.com Published: 2022-06-03 08:22:28 09月23日 Update

This article shows you how to understand JavaScript json objects, the content is concise and easy to understand, can definitely brighten your eyes, through the detailed introduction of this article, I hope you can get something.

I. the syntax of the object

The JSON object is surrounded by curly braces {}, and the JSON object is written as key/value.

{"name": "John", "age": 30, "car": null}

Key must be a string, and value must be a valid JSON data type (string, number, object, array, Boolean, or empty), Keys and values are separated by colons, and each key/value pair is separated by a comma.

Second, the value of the access object

You can use the dot (.) to access the object value.

Project

Access a JSON object using dot notation:

Var myObj, x; myObj = {"name": "John", "age": 30, "car": null}; x = myObj.name; document.getElementById ("demo") [xss_clean] = x

You can also access object values using square brackets ([]):

Var myObj, x; myObj = {"name": "John", "age": 30, "car": null}; x = myObj ["name"]; document.getElementById ("demo") [xss_clean] = x

Third, loop an object

You can use a for-in loop to traverse an object.

Var myObj = {"name": "John", "age": 30, "car": null}; for (x in myObj) {document.getElementById ("demo") [xss_clean] + = x + "

";}

Access property values using parenthesis notation:

Var myObj, x; myObj = {"name": "John", "age": 30, "car": null}; x = myObj ["name"]; document.getElementById ("demo") [xss_clean] = x

4. Nested JSON objects

The value of JSON can be another JSON object.

MyObj = {"name": "John", "age": 30, "cars": {"car1": "Ford", "car2": "BMW", "car3": "Fiat"}}

You can access nested JSON objects using dot symbols or parentheses:

X = myObj.cars.car2; / / or: X = myObj.cars ["car2"]

1. Modify valu

You can use dot notation to modify any value of the JSON object:

MyObj.cars.car2 = "Mercedes"

Js complete code:

Var myObj, I, x = ""; myObj = {"name": "John", "age": 30, "cars": {"car1": "Ford", "car2": "BMW", "car3": "Fiat"} myObj.cars.car2 = "Mercedes" For (I in myObj.cars) {x + = myObj.cars [I] + "

";} document.getElementById (" demo ") [xss_clean] = x

You can also use square brackets to modify a JSON object value:

MyObj.cars ["car2"] = "Mercedes"

Js complete code:

Var myObj, I, x = ""; myObj = {"name": "John", "age": 30, "cars": {"car1": "Ford", "car2": "BMW", "car3": "Fiat"} myObj.cars ["car2"] = "Mercedes"; for (I in myObj.cars) {x + = myObj.cars [I] + "

";} document.getElementById (" demo ") [xss_clean] = x

two。 Delete the properties of an object

Use the delete keyword to delete an attribute from the JSON object:

Delete myObj.cars.car2

Complete code:

Project

How to delete properties of a JSON object.

Var myObj, I, x = ""; myObj = {"name": "John", "age": 30, "cars": {"car1": "Ford", "car2": "BMW", "car3": "Fiat"} delete myObj.cars.car2; / / delete content through statements. For (I in myObj.cars) {x + = myObj.cars [I] + "

";} document.getElementById (" demo ") [xss_clean] = x

The above is how to understand JavaScript json objects. Have you learned any knowledge or skills? If you want to learn more skills or enrich your knowledge reserve, you are welcome to follow the industry information channel.

Tags: Objects code content attributes parentheses characters strings skills square brackets knowledge representations items loops valid concise concise keys keywords colons that is Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno OPPO Reno Docker Shulou Information macOS Microsoft