限制JSON数据



我正在从API获取数据,并尝试重新构造数据。我希望数据te以一种JSON的方式进行结构化。如下图所示。

我已经尝试了很多,但我发现很难做到,因为对于每个方法和使用的for循环。

我该怎么做?如有任何帮助,我们将不胜感激!

我想将程序的输出更改为这种类型的json结构:

[ 
{ id: 'bitcoin',
x: [1571695345012, 1571699061041, 1571702676146, 1571706278232, 1571709762429],
y: [8222.065325961827, 8192.012381921422, 8220.91853113097, 8192.5487839435, 8184.743590188011] 
}
{ id: 'ethereum',
x: [1571695472168, 1571699049447, 1571702639502, 1571706279834, 571709827389],
y: [174.5266477952419, 173.6409574561425, 174.35449608806442, 173.9800501560514, 173.99298281578433]
}
{ id: 'ripple',
x: [1571695472168, 1571699049447, 1571702639502, 1571706279834, 571709827389],
y: [0.2926270710439499, 0.29225262544982944, 0.2922858993183195, 0.29169629356590593, 0.2926150467160304]
}
]

这是我现在的输出:

[ { id: 'bitcoin' },
{ x: 1571695345012 },
{ x: 1571699061041 },
{ x: 1571702676146 },
{ x: 1571706278232 },
{ x: 1571709762429 },
{ y: 8222.065325961827 },
{ y: 8192.012381921422 },
{ y: 8220.91853113097 },
{ y: 8192.5487839435 },
{ y: 8184.743590188011 },
{ id: 'ethereum' },
{ x: 1571695472168 },
{ x: 1571699049447 },
{ x: 1571702639502 },
{ x: 1571706279834 },
{ x: 1571709827389 },
{ y: 174.5266477952419 },
{ y: 173.6409574561425 },
{ y: 174.35449608806442 },
{ y: 173.9800501560514 },
{ y: 173.99298281578433 },
{ id: 'ripple' },
{ x: 1571695366383 },
{ x: 1571699042366 },
{ x: 1571702671612 },
{ x: 1571706274116 },
{ x: 1571709875603 },
{ y: 0.2926270710439499 },
{ y: 0.29225262544982944 },
{ y: 0.2922858993183195 },
{ y: 0.29169629356590593 },
{ y: 0.2926150467160304 } ]

使用的代码:

//Initialize unirest
var unirest = require("unirest");
//Combined array
var JSONApiData = ['bitcoin', 'ethereum', 'ripple'];
var ApiSparkline = []; // *** Should this really be a module global??
function unirestp(...args) {
return new Promise((resolve, reject) => {
unirest(...args).end(res => {
if (res.error) {
reject(res.error);
} else {
resolve(res.body);
}
});
});
}
async function request() {
console.log("waiting for the sparkline data...");
// Runs the API requests in parallel
const data = await Promise.all(JSONApiData.map(id =>
unirestp("GET", `https://api.coingecko.com/api/v3/coins/${id}/market_chart?vs_currency=usd&days=7`)
));
// Promise.all guarantees that the result array is in the same order as the input array, so
// we can rely on that when mapping the reults
data.forEach((id, index) => {

const {prices} = data[index];
ApiSparkline.push({id: JSONApiData[index]});
for (let j = 0; j < 5; ++j) {        // *** What if `prices` doesn't have 5 entries?
ApiSparkline.push({x:prices[j][0]});
}
for (let j = 0; j < 5; ++j) {        // *** What if `prices` doesn't have 5 entries?
ApiSparkline.push( {y:prices[j][1]});
}
});
// Show object
console.log(ApiSparkline);
}
//Call request function
request()
.then(() => {
// Done
})
.catch(error => {
console.log(error)
// Handle/report error
});

每个API调用(即每个(只需要调用ApiSparkline.push一次

//Initialize unirest
const unirest = require("unirest");
//Combined array
const JSONApiData = ['bitcoin', 'ethereum', 'ripple'];
function unirestp(...args) {
return new Promise((resolve, reject) => {
unirest(...args).end(res => {
if (res.error) {
reject(res.error);
} else {
resolve(res.body);
}
});
});
}
async function request() {
console.log("waiting for the sparkline data...");
// Runs the API requests in parallel
const data = await Promise.all(JSONApiData.map(id =>
unirestp("GET", `https://api.coingecko.com/api/v3/coins/${id}/market_chart?vs_currency=usd&days=7`)
));
// Promise.all guarantees that the result array is in the same order as the input array, so
// we can rely on that when mapping the reults
const ApiSparkline = [];
data.forEach((result, index) => {
const { prices } = result;
const id = JSONApiData[index];
const x = prices.map(([xItem, yItem]) => xItem);
const y = prices.map(([xItem, yItem]) => yItem);
ApiSparkline.push({
id,
x,
y
});
});
return ApiSparkline;
}
//Call request function
request()
.then(ApiSparkline => {
console.log(ApiSparkline);
})
.catch(error => {
console.log(error)
// Handle/report error
});

最新更新