accessing objects in $.getJSON

So, i used $.getJSON to get the objects from openweathermap API. Below is what I get in my console when I try to type:

console.log(data[0])

I want to access 'icon'. I tried:

data[0]["icon"] and data[0].icon

However, it was to no avail.

Can anyone help me? Thanks in advance!

$.getJSON

(edit #1) Here is my code:

$.getJSON(weatherlink, function(json1) {
    $.each(json1, function(key, data) {
        // Creating a marker and putting it on the map
        var marker = new MarkerWithLabel({
            position: points,
        labelContent: title,
        labelAnchor: new google.maps.Point(22, 20),
        labelClass: "label",
            // icon: "http://openweathermap.org/img/w/" +data[0]["icon"]+ ".png"
        });
    markersWeather.push(marker);
    marker.setMap(map);
    console.log(data[0]);
    });
})

weatherlink is a variable containing the link to the API.

(edit #2) this is how the entire data looks like

(edit #3) it is giving me undefined when i do either way.

Answers


Start at json1[0] instead of data:

var icon;

json1 = [{
  "icon": "04n"
}];

$.each(json1, function() {

  icon = "<img src = http://openweathermap.org/img/w/" + json1[0].icon + ".png>"
});

$('#output').html(icon);
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div id="output"></div>

Try this(data[key]['icon']):

$.getJSON(weatherlink, function(json1) {
    $.each(json1, function(key, data) {
        // Creating a marker and putting it on the map
        var marker = new MarkerWithLabel({
            position: points,
        labelContent: title,
        labelAnchor: new google.maps.Point(22, 20),
        labelClass: "label",
            // icon: "http://openweathermap.org/img/w/" +data[0]["icon"]+ ".png"
        });
    markersWeather.push(marker);
    marker.setMap(map);
    console.log(data[key]['icon']);
    });
})

json1 (Object) is inside loop you can use a condition like if ( key == 'icon' ) or if ( json1.hasOwnProperty( 'icon' ) ), if it matches a condition use variable like data or json1[key]

If you want to get the value outside of the loop, use json1['icon'].


Need Your Help

System.ArgumentOutOfRangeException Toolstrip menu

c# winforms toolstripmenu

I am getting the below exception randomly. The toolstrip menu is created dynamically.