Problem with loadJSONArray example

Hello,

I am trying to run the simple example for “loadJSONArray” found here:
https://processing.org/reference/loadJSONArray_.html

When I do so (by copying and pasting into a Processing sketch, and also creating the data.json file specified in the comments), I get the error:
“cannot convert from processing.data.JSONArray to JSONArray”

What is causing this and how can I fix it?

Many thanks in advance,
Bill

i also just try here
( win 7 / 64bit / processing 3.5.3 / java mode )
and it works.
-a- the file should be at subdir

data/data.json

and it should look like

  [
    {
      "id": 0,
      "species": "Capra hircus",
      "name": "Goat"
    },
    {
      "id": 1,
      "species": "Panthera pardus",
      "name": "Leopard"
    },
    {
      "id": 2,
      "species": "Equus zebra",
      "name": "Zebra"
    }
  ]

means all that // must be deleted
the example documentation should
use

/*
code
*/

instead

// code

what is difficult to copy!
https://github.com/processing/processing-docs/issues/748 (reported)


a general remark;
the

loadXXX(file);

uses a friendly trick looking up the file at

data/file

as soon you once use

loadXXX(file);
saveXXX(file);

you learn that the trick works for load but not for save!
so please:
whenever use a filename make it incl path

String filename = "data/file.ext"

and you can do load and save without thinking

Are you in Java mode? You should…

I didn’t have any issues running this

@kll remove all // in a section: select with mouse, Menu Edit | Comment/Uncomment…

Chrisir

JSONArray values;

void setup() {
  size(400, 400); 

  values = loadJSONArray("data.json");

  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);
  }
}
//

pls read my above post again, where i say

please detail how you make the file, possibly we not know some tricks about the
processing 3.5.3 IDE / editor

I know, you got it working and I just agreed.

I am referring to:

please detail how you make the file, possibly we not know some tricks about the
processing 3.5.3 IDE / editor

and to:

means all that // must be deleted
the example documentation should
use

/*
code
*/

maybe I got you wrong

found here:
https://processing.org/reference/loadJSONArray_.html

paste into processing

// [
//   {
//     "id": 0,
//     "species": "Capra hircus",
//     "name": "Goat"
//   },
//   {
//     "id": 1,
//     "species": "Panthera pardus",
//     "name": "Leopard"
//   },
//   {
//     "id": 2,
//     "species": "Equus zebra",
//     "name": "Zebra"
//   }
// ]

remove all // in a section: select with mouse, Menu Edit | use Comment/Uncomment…

Chrisir

we know that, and the main thing what i pointed out was that that remove is necessary
for reading the file.

still, you

  • copy paste the whole section from reference to your sketch
  • you uncomment the data part
  • AND THEN?

how you make that section to a file with the IDE editor, please details as i never do this?
always use my filemanager / make the data subdir / and use external editor,
COPY PASTE

I do it the same way :wink:

I am using Processing 3.5.3 and using the IDE on a Mac. I am using Java mode.

The code I have in the sketch is:

JSONArray values;

void setup() {

  values = loadJSONArray("data.json");

  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);
  }
}

In the data folder I have a file “data.json” that contains:

[
   {
     "id": 0,
     "species": "Capra hircus",
     "name": "Goat"
   },
   {
     "id": 1,
     "species": "Panthera pardus",
     "name": "Leopard"
   },
   {
     "id": 2,
     "species": "Equus zebra",
     "name": "Zebra"
   }
 ]

On the Mac, the produces the error cited in my original post:
“cannot convert from processing.data.JSONArray to JSONArray”

However, I tried this on a Windows machine and it ran fine.

1 Like

sorry can not help about MAC,
just hope someone with that hardware can jump in,

but you should be much more detailed about
hardware and OS revision …
add can you prepare and run

void mysysinfo() {
  println( "System     : " + System.getProperty("os.name") + "  " + System.getProperty("os.version") + "  " + System.getProperty("os.arch") );
  println( "JAVA       : " + System.getProperty("java.home")  + " rev: " +javaVersionName);
  println( "user.home  : " + System.getProperty("user.home") );
  println( "user.dir   : " + System.getProperty("user.dir") );
  println( "sketchPath : " + sketchPath() );
  println( "dataPath   : " + dataPath("") );
  println( "dataFile   : " + dataFile("") );
}

void setup() {
  mysysinfo();
}

and instead of try to build up from “reference”
just run directly from examples, is faster, less error prone
from example test:
… processing-3.5.3\modes\java\examples\Topics\Advanced Data\LoadSaveJSON
in case its a " build for MAC " problem there could be much more problems.

I am able to run the “LoadSaveJSON” example you cite, but it does not involve the “loadJSONArray” method that is provoking the error…

Did you try

processing.data.JSONArray instead of JSONArray

as the error message suggests?

Chrisir

How would that be done?

Hmm… Any chance you have another library loaded that defines a JSONArray object? Imported, installed through contributions manager, or in a local jar?

What version of PDE are you running on mac, and what are the installed libraries?

1 Like