Welcome to the Treehouse Community
Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.
Looking to learn something new?
Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.
Start your free trialGabriel E
8,916 PointsGet property from JSON object?
This probably sounds like a stupid title to this question, but I have a variable that contains a url which gets your location: http://ip-api.com/json Not always accurate, but that of course depends on the user, not the api itself. Now, I ripped out the location finder from part of another api, and I wish to include this in it instead. Essentially: firstHalfofApiMentionedAbove + location(the one I included a link to) + secondHalfOfApiMentionedAbove. But this obviously just sticks the url into the api, and I need to just stick the property keys I want into the api, not the full url. The properties I wish to include are, for example, 'city' and 'region'. Thanks a ton in advance!
~ G
Gabriel E
8,916 PointsTerribly sorry, my esc key broke so I don't know how to use the markup sheet without it. I'll just post it and if you can't read it that well I'll try to find another way.
var location = 'http://ip-api.com/json';
$.getJSON(apiHalf1 + location + tempM + apiHalf2).done(function(data) { $('.current-weather').html(Math.ceil(data['main']['temp']) + '°').css({ 'font-size': '4em', 'color': '#3f3f3f' }); });
--- see how I'm passing in the location as an entire url? That's what I can't have, it needs to just be specific object properties from the JSON.
1 Answer
Brendan Whiting
Front End Web Development Techdegree Graduate 84,736 PointsYou need to make an AJAX request to the first API, 'http://ip-api.com/json', to get the data from that server, and when you get the response, your callback will fire, which should contain a second AJAX request to the other API. I'm putting in the 'city' and 'region' properties with a comma between them in this example.
I decided to call the two arguments for the callbacks data1
and data2
so it's clear that they're different. Also, FYI, you can access the 'main' and 'temp' properties with dot notation ("data2.main.temp") as well and it's better to do it this way unless you have to do it the other way.
$.getJSON('http://ip-api.com/json', function(data1) {
$.getJSON(apiHalf1 + data1.city + ',' + data1.region + tempM + apiHalf2, function(data2) {
$('.current-weather').html(Math.ceil(data2['main']['temp']) + '°').css({
'font-size': '4em',
'color': '#3f3f3f'
});
});
});
Gabriel E
8,916 PointsPerfect! Thanks a lot for all your help.
G
Brendan Whiting
Front End Web Development Techdegree Graduate 84,736 PointsBrendan Whiting
Front End Web Development Techdegree Graduate 84,736 PointsCan you post some code? Even if it has pieces missing and some comments about what you want to do that would help.