Parsing total funding out of this JSON file
I'm trying to parse the total funding dollar amount out of this JSON file:
http://api.crunchbase.com/v/1/company/airbnb.js
Basically I'd like to add up all the raised_amount's in the file.
This is how I parse the oth开发者_JAVA技巧er data and that works (that works!):
$.ajax({
url: "http://api.crunchbase.com/v/1/company/airbnb.js",
dataType: 'jsonp',
success: function(results){
var number_of_employees = results.number_of_employees;
var founded_month = results.founded_month;
var founded_year = results.founded_year;
$('#number_of_employees').append(number_of_employees);
$('#founded').append(founded_month + '/' + founded_year);
}
});
Thanks!
Where obj
is your JSON object:
var totalAmount = 0;
var fr = obj.funding_rounds;
for(var i = 0; i < fr.length; i++){
totalAmount += fr[i].raised_amount;
}
http://jsfiddle.net/8XPKq/
// convert the format
// disclaimer: there are better ways to do this!
var currencyTotal;
if(totalAmount > 999999){
currencyTotal = (totalAmount / 1000000).toFixed(1);
currencyTotal += "M";
}
else if(totalAmount > 999){
currencyTotal = (totalAmount / 1000).toFixed(1);
currencyTotal += "K";
}
alert(totalAmount);
http://jsfiddle.net/8XPKq/1/
精彩评论