json - 在处理中访问嵌套的 JSON
问题描述
我需要从 JSON 文件中提取 3 个值。我只是设法获取所有数据,但我无法将它们分开。该文件如下所示:
{
"motionAndDeviceRelated":{
"mOrientation":[0,0,0],
"mLocalVelocity":[0,0,0],
"mWorldVelocity":[0,0,0],
"mAngularVelocity":[0,0,0],
"mLocalAcceleration":[0,0,0],
"mWorldAcceleration":[0,0,0],
"mExtentsCentre":[0,0,0]
}
}
我需要从 mLocalAcceleration 中提取浮点变量数据。
import http.requests.*;
JSONObject json;
void setup() {
size(600,600);
}
void draw() {
// actually returns something instead of throwing an error, which is progress
long t = System.currentTimeMillis();
GetRequest get = new GetRequest("http://" + "localhost:8180/crest2/v1/api?motionDeviceRelated=true&formatted=true");
get.send();
JSONObject json = parseJSONObject(get.getContent());
println(json);
println(System.currentTimeMillis() - t);
loop();
}
解决方案
这是有效的代码:
`import http.requests.*;
JSONObject json;
float x;
float y;
float z;
void setup() {
size(800,800);
}
void draw() {
long t = System.currentTimeMillis();
GetRequest get = new GetRequest("http://" + "localhost:8180/crest2/v1/api?motionDeviceRelated=true&formatted=true");
get.send();
JSONObject json = parseJSONObject(get.getContent());
JSONObject motion = json.getJSONObject("motionAndDeviceRelated");
JSONArray acc = motion.getJSONArray("mWorldAcceleration");
x = acc.getFloat(0);
z = acc.getFloat(1);
y = acc.getFloat(2);
println(System.currentTimeMillis() - t);
loop();
}`
推荐阅读
- javascript - 如果元素属于 A 类但不是 B 类,则悬停时 Jquery 调用函数
- javascript - 通过类名而不是 ID 获取元素
- python - aioredis.Channel.wait_message() 抛出的异常是什么?
- amazon-web-services - 关于AWS VPC的几个概念问题
- node.js - 使用 googleOauth 和护照重定向到上一页
- sql - 根据 ID 创建新的 Event_ID,并在日期列上有滑动窗口
- javascript - CTRL+C 后的 Docker 错误:无法杀死容器
- scala - giter8 使用 '\$' 转义
- file - Apache Camel 使用新行令牌拆分并使用聚合策略
- c# - 如何使用 Html Agility Pack 查找表格的最后一列