从 JSON 中提取信息:P5.js [英] Extracting info from a JSON: P5.js

查看:73
本文介绍了从 JSON 中提取信息:P5.js的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的 php 脚本 json_encodes 这个:

my php script json_encodes this:

[{"x":"20","y":"24","name":"NewNov"},{"x":"20","y":"70","name":"Tito"}]

但是我看不到如何在我的 p5.js 程序中提取这些信息?

But I can't see how I can extract this information in my p5.js program?

比如说,我需要使用那些 'x'、'y'、'name' 在适当的地方用正确的名字画一个圆圈.

Say, I need to use those 'x', 'y', 'name' to draw a circle in the appropriate place with the right name.

我在脚本中使用了 loadJSON,现在我有了一个变量 -

I used loadJSON in the script, and now I have a variable -

data = loadJSON()

但是我如何从 JSON 中获取x"组件?

But how do I get, say, the 'x' component from the JSON?

var radius;

function preload() {
    var url = 'http://localhost/planetTrequest.php';
    radius = loadJSON(url);
}

function setup() {
    createCanvas(300, 300);
    background(153);
    noLoop();
}

function draw() {
    background(200);
    textSize(15);
    text(radius.x, 10, 30);
    fill(0, 102, 153);
}

推荐答案

感谢以上所有评论,这就是最终奏效的方法:

Thanks to all the comments above, that's what worked in the end:

 var data;

 function preload() {
 var url = 'http://localhost/planetTrequest.php';
 data = loadJSON(url);
 }

 function setup() {
 createCanvas(300, 300);
 background(153);
 noLoop();
 }

 function draw() {
 background(200);
 textSize(15);
 text(data[0].name, 10, 30);
 fill(0, 102, 153);
 }  

这篇关于从 JSON 中提取信息:P5.js的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆