当前位置: 首页>>代码示例 >>用法及示例精选 >>正文


Processing JSONObject.getJSONArray()用法及代码示例


Processing, 类JSONObject中的getJSONArray()用法介绍。

用法

  • .getJSONArray(key)

参数

  • key (String) 一个关键字符串

返回

  • JSONArray

说明

检索带有关联 key 的JSONArray

例子

// The following short JSON file called "data.json" is parsed 
// in the code below. It must be in the project's "data" folder.
//
// { "animals": [
//   {
//     "id": 0,
//     "species": "Capra hircus",
//     "name": "Goat"
//   },
//   {
//     "id": 1,
//     "species": "Panthera pardus",
//     "name": "Leopard"
//   },
//   {
//     "id": 2,
//     "species": "Equus zebra",
//     "name": "Zebra"
//   } ]
// }

JSONObject json;

void setup() {

  json = loadJSONObject("data.json");

  JSONArray values = json.getJSONArray("animals");
  
  for (int i = 0; i < values.size(); i++) {
    
    JSONObject animal = values.getJSONObject(i); 

    int id = animal.getInt("id");
    String species = animal.getString("species");
    String name = animal.getString("name");

    println(id + ", " + species + ", " + name);
  }
}

// Sketch prints:
// 0, Capra hircus, Goat
// 1, Panthera pardus, Leopard
// 2, Equus zebra, Zebra

相关用法


注:本文由纯净天空筛选整理自processing.org大神的英文原创作品 JSONObject.getJSONArray()。非经特殊声明,原始代码版权归原作者所有,本译文未经允许或授权,请勿转载或复制。