It is worth noting that JSON data types are directly mapped to standard javascript data types. This means that there is no such thing as a JSON array after the JSON.parse() , then you will get simple javascript primitives: arrays, objects, strings, numbers, etc.
Thus, getting an element from a JSON array is simply accessing an element of a standard javascript array. This is usually achieved using the square bracket operator var element = array[index] .
However, your code is broken into several parts that are not related to accessing an array element.
First, you define the data variable at the top level - you just don't need it there (at least according to the sample).
Then you initialize it with an empty array - I have no idea why.
The onreadystatechange is simple when expressed in words: you get a string, parse it as json, and then call getName , providing the result. This means that your getName() should need the data argument, which you then get as part of the function.
source share