如何使用 json 中的变量调用 preload() 函数中的另一个 json?
How to use a variable from a json to call another json in preload() function?
我在 javascript 中使用 p5js 库。
我使用 wrapAPI 制作了一些自定义 APIs 以从网络上获取 posts 和评论。
ID 为 post 的第一个 API returns 最近 post 个。 post 的第二个 API returns 评论使用第一个 API 调用的 ID。
这样做的好方法是什么?
我的想法是将 preload()
中的 idvox
和 categoria
替换为:
idvox = datav.data.vox[1].idvox;
但是第二个loadJSON()
函数不起作用。如果我尝试 console.log 或使用 post id 调用变量,则会返回一个空的 Object
或 undefined
值。
function preload(){
var datav = loadJSON('https://wrapapi.com/use/example/example/example/latest?wrapAPIKey=API_KEY');
categoria = datav.data.vox[1].category;
idvox = datav.data.vox[1].idvox;
dataC = loadJSON('https://wrapapi.com/use/example/example/example/0.0.5?categoria='+categoria+'&idvox='+idvox+'&wrapAPIKey=API_KEY');
}
为了将来自一个 loadJSON
调用的数据用作第二个调用的参数,您需要考虑到这些是异步调用。参见 Loading-external-files:-AJAX, -XML, -JSON
您将在回调函数中卸载数据,而不是直接将变量设置为 return 值:
Callbacks
A callback is a function that is passed to another function as a parameter, and called by that other function. A callback function is useful when working with asynchronous functions because it allows us to specify some code to execute after the first asynchronous task has completed.
We've actually already used callbacks when we used setTimeout, setInterval, and addEventListener. For example, "doSomething" below is a callback function:
function doSomething() {
console.log("doing something!");
}
setTimeout(doSomething, 5000);
You will see callbacks used in p5.js and jQuery to tell the program what to do after the external data is received.
function setup() {
loadJSON("data.json", drawData);
}
loadJSON
loadJSON loads a JSON file and returns a JavaScript object. It takes two arguments, the path to the file, and the callback function. When the server has returned the JSON data and it has been parsed, drawData is run with the result automatically passed in as the variable "data".
function drawData(data) {
// person 1 bubble
fill(155, 30, 180, 180);
ellipse(250, 200, data.person1.age * 5, data.person1.age * 5); // person1.age = 30
fill(255);
text(data.person1.name, 210, 200); // person1.name = Morgan
// person 2 bubble
fill(180, 180, 34, 180);
ellipse(350, 200, data.person2.age * 5, data.person2.age * 5); // person2.age = 32
fill(255);
text(data.person2.name, 330, 200); // person2.name = Joss
}
为了您的目的,您将采用这样的代码来链接您的 AJAX 调用。在您的情况下,您将有一个进行第二次调用的函数,您将把它传递给对 loadJSON
的第一次调用。第二次调用还将传入一个回调函数,您将使用该回调函数卸载最终数据。
我在 javascript 中使用 p5js 库。 我使用 wrapAPI 制作了一些自定义 APIs 以从网络上获取 posts 和评论。
ID 为 post 的第一个 API returns 最近 post 个。 post 的第二个 API returns 评论使用第一个 API 调用的 ID。
这样做的好方法是什么?
我的想法是将 preload()
中的 idvox
和 categoria
替换为:
idvox = datav.data.vox[1].idvox;
但是第二个loadJSON()
函数不起作用。如果我尝试 console.log 或使用 post id 调用变量,则会返回一个空的 Object
或 undefined
值。
function preload(){
var datav = loadJSON('https://wrapapi.com/use/example/example/example/latest?wrapAPIKey=API_KEY');
categoria = datav.data.vox[1].category;
idvox = datav.data.vox[1].idvox;
dataC = loadJSON('https://wrapapi.com/use/example/example/example/0.0.5?categoria='+categoria+'&idvox='+idvox+'&wrapAPIKey=API_KEY');
}
为了将来自一个 loadJSON
调用的数据用作第二个调用的参数,您需要考虑到这些是异步调用。参见 Loading-external-files:-AJAX, -XML, -JSON
您将在回调函数中卸载数据,而不是直接将变量设置为 return 值:
Callbacks
A callback is a function that is passed to another function as a parameter, and called by that other function. A callback function is useful when working with asynchronous functions because it allows us to specify some code to execute after the first asynchronous task has completed.
We've actually already used callbacks when we used setTimeout, setInterval, and addEventListener. For example, "doSomething" below is a callback function:
function doSomething() {
console.log("doing something!");
}
setTimeout(doSomething, 5000);
You will see callbacks used in p5.js and jQuery to tell the program what to do after the external data is received.
function setup() {
loadJSON("data.json", drawData);
}
loadJSON
loadJSON loads a JSON file and returns a JavaScript object. It takes two arguments, the path to the file, and the callback function. When the server has returned the JSON data and it has been parsed, drawData is run with the result automatically passed in as the variable "data".
function drawData(data) {
// person 1 bubble
fill(155, 30, 180, 180);
ellipse(250, 200, data.person1.age * 5, data.person1.age * 5); // person1.age = 30
fill(255);
text(data.person1.name, 210, 200); // person1.name = Morgan
// person 2 bubble
fill(180, 180, 34, 180);
ellipse(350, 200, data.person2.age * 5, data.person2.age * 5); // person2.age = 32
fill(255);
text(data.person2.name, 330, 200); // person2.name = Joss
}
为了您的目的,您将采用这样的代码来链接您的 AJAX 调用。在您的情况下,您将有一个进行第二次调用的函数,您将把它传递给对 loadJSON
的第一次调用。第二次调用还将传入一个回调函数,您将使用该回调函数卸载最终数据。