Can a JSON object be a list?
Ngày đăng:
22/10/2021
Trả lời:
0
Lượt xem:
134
JSON Array Litterals Previous NextThis is a JSON string:'["Ford", "BMW", "Fiat"]' Show
Inside the JSON string there is a JSON array literal:["Ford", "BMW", "Fiat"] Arrays in JSON are almost the same as arrays in JavaScript. In JSON, array values must be of type string, number, object, array, boolean or null. In JavaScript, array values can be all of the above, plus any other valid JavaScript expression, including functions, dates, and undefined. JavaScript ArraysYou can create a JavaScript array from a literal: ExamplemyArray = ["Ford", "BMW", "Fiat"]; Try it Yourself »You can create a JavaScript array by parsing a JSON string: ExamplemyJSON = '["Ford", "BMW", "Fiat"]';myArray = JSON.Parse(myJSON); Try it Yourself »Accessing Array ValuesYou access array values by index: ExamplemyArray[0];Try it Yourself »Arrays in ObjectsObjects can contain arrays: Example{"name":"John","age":30,"cars":["Ford", "BMW", "Fiat"]}You access array values by index: ExamplemyObj.cars[0];Try it Yourself »Looping Through an ArrayYou can access array values by using a for in loop: Examplefor (let i in myObj.cars) { x += myObj.cars[i];} Try it Yourself »Or you can use a for loop: Example for (let i = 0; i < myObj.cars.length; i++) { x += myObj.cars[i];} Try it Yourself » Previous NextVideo liên quan |