最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - JSON - There are no child objects - Stack Overflow

programmeradmin1浏览0评论

I am trying to retrieve some specific data, using jQuery to retrieve a JSON Feed.

This is what I am currently doing:

var url  = '.json?callback=?';

$.getJSON(url, function(d){
    var data = d['current_observation'];
    console.dir(data['display_location']);
});

This successfully returns to the console:

city ==> "Tokyo"
country ==> "JP"
country_iso3166 ==> "JP"
elevation  ==> "8.00000000"
full ==> "Tokyo, Japan"
latitude ==> "35.54999924"
etc...

However, let's say I want to get just the "full" name. If I try:

  console.dir(data['display_location']['full']);

I end up getting the result: There are no child objects

Any ideas on what I am doing wrong here?

I am trying to retrieve some specific data, using jQuery to retrieve a JSON Feed.

This is what I am currently doing:

var url  = 'https://api.wunderground./api/myapicode/conditions/forecast/q/Tokyo.json?callback=?';

$.getJSON(url, function(d){
    var data = d['current_observation'];
    console.dir(data['display_location']);
});

This successfully returns to the console:

city ==> "Tokyo"
country ==> "JP"
country_iso3166 ==> "JP"
elevation  ==> "8.00000000"
full ==> "Tokyo, Japan"
latitude ==> "35.54999924"
etc...

However, let's say I want to get just the "full" name. If I try:

  console.dir(data['display_location']['full']);

I end up getting the result: There are no child objects

Any ideas on what I am doing wrong here?

Share Improve this question asked Aug 27, 2012 at 18:15 DodinasDodinas 6,80522 gold badges78 silver badges109 bronze badges
Add a ment  | 

4 Answers 4

Reset to default 6

console.dir displays the properties (child objects) of the object you pass it.
It doesn't make sense to call it with a string.

You should call console.log instead.

in order to use console.dir(arg) arg should be an object. You are accessing a full key of an object in console.dir(data['display_location']['full']); which is a plain string. use console.log(data['display_location']['full']) instead

You should be using console.log() to get a value instead of an object's properties.

console.dir will show object trees - the properties of the object you pass in. Yet, the property you log is just a string and has no child objects. Use console.log instead.

发布评论

评论列表(0)

  1. 暂无评论