В javascript я хочу получить данные в соответствии с моими требованиями, но я не знаю, как преобразовать данные. Я получаю данные из Fitbit, и в ответ веб-API дают данные json, и я хочу изменить эти данные с помощью javascript.
Мой ответ API показан ниже.
0: {date: "2019-4-25"}
1: {distance: "1.0627499999999999"}
2: {minutes: "30"}
3: {calories: "19"}
4: {steps: "750"}
и я хочу получить данные лайков: -
"2019-04-25":{distance: "0.7085",steps: "500"minutes: "20",calories: "187"}
мой код JavaScript показан ниже, пожалуйста, помогите мне, как получить данные внутри объекта в соответствии с данными ...
app.js
let date = new Date();
let todayDate = `${date.getFullYear()}-${date.getMonth() + 1}-${date.getDate()}`;
// let endDate =`${date.getFullYear()}-${date.getMonth() + 1}-${date.getDate() + 1}`;
// console.log(endDate);
var url = window.location.href;
//getting the access token from url
var access_token = url.split("#")[1].split("=")[1].split("&")[0];
// get the userid
var userId = url.split("#")[1].split("=")[2].split("&")[0];
// console.log(access_token);
var response = [];
var key = "date";
var obj = {};
obj[key] = todayDate;
response.push(obj);
console.log(response);
// Make an API request and graph it
var processResponse = function (res) {
if (!res.ok) {
throw new Error('Fitbit API request failed: ' + res);
}
var contentType = res.headers.get('content-type')
if (contentType && contentType.indexOf("application/json") !== -1) {
return res.json();
} else {
throw new Error('JSON expected but received ' + contentType);
}
}
var xhr = new XMLHttpRequest();
xhr.open('GET', 'https://api.fitbit.com/1/user/' + userId + '/activities/steps/date/today/1d.json');
xhr.setRequestHeader("Authorization", 'Bearer ' + access_token);
xhr.onload = function () {
if (xhr.status === 200) {
var d1 = xhr.responseText;
var jsonResponse = JSON.parse(d1);
var steps = jsonResponse["activities-steps"][0].value;
console.log("steps:", steps);
var key = "steps";
var obj = {};
obj[key] = steps;
response.push(obj);
}
};
xhr.send();
var xhr2 = new XMLHttpRequest();
xhr2.open('GET', 'https://api.fitbit.com/1/user/' + userId + '/activities/distance/date/today/1d.json');
xhr2.setRequestHeader("Authorization", 'Bearer ' + access_token);
xhr2.onload = function () {
if (xhr2.status === 200) {
var d2 = xhr2.responseText;
var jsonResponse = JSON.parse(d2);
var distance = jsonResponse["activities-distance"][0].value;
var key = "distance";
var obj = {};
obj[key] = distance;
response.push(obj);
console.log("distance:", distance);
}
};
xhr2.send();
var xhr3 = new XMLHttpRequest();
xhr3.open('GET', 'https://api.fitbit.com/1/user/' + userId + '/activities/activityCalories/date/today/1d.json');
xhr3.setRequestHeader("Authorization", 'Bearer ' + access_token);
xhr3.onload = function () {
if (xhr3.status === 200) {
var d3 = xhr3.responseText;
var jsonResponse = JSON.parse(d3);
var calories = jsonResponse["activities-activityCalories"][0].value;
var key = "calories";
var obj = {};
obj[key] = calories;
response.push(obj);
console.log("calories:", calories);
//document.write(xhr2.responseText);
}
};
xhr3.send();
var xhr4 = new XMLHttpRequest();
xhr4.open('GET', 'https://api.fitbit.com/1/user/' + userId + '/activities/minutesVeryActive/date/today/1d.json');
xhr4.setRequestHeader("Authorization", 'Bearer ' + access_token);
xhr4.onload = function () {
if (xhr4.status === 200) {
var d4 = xhr4.responseText;
var jsonResponse = JSON.parse(d4);
var minutes = jsonResponse["activities-minutesVeryActive"][0].value;
var key = "minutes";
var obj = {};
obj[key] = minutes;
response.push(obj);
console.log("minutes:", minutes);
}
};
xhr4.send();