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

How to understand JavaScript json object

2025-01-27 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

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

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.

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

Development

Wechat

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

12
Report