Arrays 处理错误消息';函数getJSONArray(JSONArray)不存在';
当我运行这个使用json查询的草图时,我不确定为什么会收到错误消息“getJSONArray(JSONArray)函数不存在”。该注释似乎不合逻辑,因为处理过程正在识别JSONArray的id ref 可以在此处阅读.json: 有什么想法吗?谢谢Arrays 处理错误消息';函数getJSONArray(JSONArray)不存在';,arrays,json,httpclient,processing,Arrays,Json,Httpclient,Processing,当我运行这个使用json查询的草图时,我不确定为什么会收到错误消息“getJSONArray(JSONArray)函数不存在”。该注释似乎不合逻辑,因为处理过程正在识别JSONArray的id ref 可以在此处阅读.json: 有什么想法吗?谢谢 import com.francisli.processing.http.*; HttpClient client; String data; com.francisli.processing.http.JSONObject weatherInfo
import com.francisli.processing.http.*;
HttpClient client;
String data;
com.francisli.processing.http.JSONObject weatherInfo;
JSONArray hourly_forecast;
int last = 0;
PImage img;
Float humidity = 50.2;
void setup() {
size(700, 700);
client = new HttpClient(this, "api.wunderground.com");
client.GET("/api/97a2805510de59e9/hourly/q/pws:IENGLAND274.json");
background(255);
}
void responseReceived(HttpRequest request, HttpResponse response) {
println(response.getContentAsString());
weatherInfo = response.getContentAsJSONObject();
JSONArray hourly_forecast = weatherInfo.getJSONArray(hourly_forecast);
}
没有名为
getJSONArray()
的方法将JSONArray作为参数
函数应该用作JSONArray.getJSONArray(0)
,它获取JSON数据中的第一个元素数组。你可以把它放在一个循环中,得到所有的
示例(摘自:):
//将解析以下名为“data.JSON”的短JSON文件
//在下面的代码中。它必须位于项目的“数据”文件夹中。
//
// [
// [
//{“name”:“apple”,“isFruit”:true},
//{“name”:“grape”,“isFruit”:true},
//{“name”:“carrot”,“isFruit”:false}
// ],
// [
//{“name”:“莴苣”,“isFruit”:false},
//{“名字”:“李子”,“是水果”:真的},
//{“name”:“肉桂”,“isFruit”:false}
// ]
// ]
JSONArray-json;
无效设置(){
json=loadJSONArray(“data.json”);
//获取元素的第一个数组
JSONArray values=json.getJSONArray(0);
对于(int i=0;i
代码的第二个问题是,您两次声明小时预测:一次是全局声明,一次是在
responseReceived()
函数中声明。您可能想从responseReceived()
函数中的hourly\u forecast
中删除JSONArray
。由于我正在加载的文件是一个包含各种JSONArray对象的JSONObject,您建议我如何“抓取”JSONArray项?
// The following short JSON file called "data.json" is parsed
// in the code below. It must be in the project's "data" folder.
//
// [
// [
// { "name": "apple", "isFruit": true },
// { "name": "grape", "isFruit": true },
// { "name": "carrot", "isFruit": false }
// ],
// [
// { "name": "lettuce", "isFruit": false },
// { "name": "plum", "isFruit": true },
// { "name": "cinnamon", "isFruit": false }
// ]
// ]
JSONArray json;
void setup() {
json = loadJSONArray("data.json");
// Get the first array of elements
JSONArray values = json.getJSONArray(0);
for (int i = 0; i < values.size(); i++) {
JSONObject item = values.getJSONObject(i);
String name = item.getString("name");
boolean isFruit = item.getBoolean("isFruit");
println(name + ", " + isFruit);
}
}
// Sketch prints:
// apple, true
// grape, true
// carrot, false