How to access JSON items?

How to access JSON items?
0

#1

If I understand correctly, list in this code is an array that consists of objects. I want to access temp in the main object and thought main.temp would do the trick, but I get an error message stating it’s undefined. Alternatively I’ve tried list[1].main.temp. What is the correct way to access JSON data?


#2

Have you tried list[0].main.temp ?


#3

Assign the JSON string to a variable, parse it with JSON.parse(), and then you can use it like any other JavaScript data. If it’s an index of an array, use array[index]; if it’s a property of an object, use object.property or object['property']. Nesting works by adding more indexes or properties (object.property[index] etc.)

Also, remember that JS arrays are zero indexed.


#4

List is an array. It appears to have at least one object. If you forgot how to get access an element, you really should review the challenges in Basic Javascript on arrays. Here is one which can help you on the question you have.