當前位置: 首頁>>編程示例 >>用法及示例精選 >>正文


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()。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。