Bye Bye Moore

PoCソルジャーな零細事業主が作業メモを残すブログ

processing でjson をパースする

JSONObject json;

void setup() {
  size(240, 80);
   //配列形式のデータはJSONArray形式にて取り扱う。
  JSONArray json = loadJSONArray("./data.json");
  println(json);

  for(int i = 0; i < json.size(); i++) {
    //JSONArray形式から要素を引っ張り出したい時はJSONObject形式にする。
    JSONObject item = json.getJSONObject(i);
    //各データをパース
    int id = item.getInt("id");
    String species = item.getString("species");
    String name = item.getString("name");
    
    //表示
    fill(0, 102, 153);
    text(id + ", " + species + ", " + name, 10, 10 + 20*i);
  }
}

f:id:shuzo_kino:20191019031906p:plain