JSONArray doesn't works...

edited December 2017 in Questions about Code

Hi everybody,

someone can explain me why this example, that I found in the reference, doesn't works!? at the end two errors are arised: "values.setJSONObject(i,animal) does not exist" and " json.setJSONArray() expect paramters like : String, JSONArray". maybe the second is a consequence of the first...then why the function ".setJSONObject()" does not exist (i don't found in the list) but is write in the example/reference?...how I can manage/create an JSONArray?...obviously I can create an array of "single" JSONObject. thanks

String[] species = { "Capra hircus", "Panthera pardus", "Equus zebra" };
String[] names = { "Goat", "Leopard", "Zebra" };

JSONArray values;

void setup() {

  values = new JSONArray();

  for (int i = 0; i < species.length; i++) {

    JSONObject animal = new JSONObject();

    animal.setInt("id", i);
    animal.setString("species", species[i]);
    animal.setString("name", names[i]);

    values.setJSONObject(i, animal);
  }

  saveJSONArray(values, "data/new.json");
}

Answers

  • hi GoToLoop,

    I had searched in the "report bugs" but with wrong "key word"...now I searched about " setJSONObject()" and I found #1827 and #1839...ok I understood. What I have now is that if I create a new sketch, and include the right example, it works...also after a "save as"; if I open my old sketch, already saved in the same directory, delete all and insert the right code this still not working (same errors)...why?...maybe is a dumb question but I don't found solution.

  • Your example seems to work fine for me.

    Maybe you have an old version of Processing that didn't support these functions?

  • hi kevinWorkman,

    I am using 3.3.6 on W10

  • you don’t have a size(660,650); command

  • expect parameters like : String, JSONArray"

    I will try this:

    values.setJSONObject(str(i), animal);

    Kf

  • edited June 2018

    (Both arrays species and names should have the same length)

    It works for me

    It works for me without str(i) [from kfrajer]

    I extended the code so that the new file gets loaded as a test (mainly in draw())

    I am on Win 10 / processing 3.3.7

    Chrisir ;-)

    String[] species = { "Capra hircus", "Panthera pardus", "Equus zebra" };
    String[] names = { "Goat", "Leopard", "Zebra" };
    
    JSONArray values;
    
    JSONArray valuesLoaded;
    
    void setup() {
    
      size(660, 700);
    
      values = new JSONArray();
    
      for (int i = 0; i < species.length; i++) {
    
        JSONObject animal = new JSONObject();
    
        animal.setInt("id", i);
        animal.setString("species", species[i]);
        animal.setString("name", names[i]);
    
        values.setJSONObject(i, animal);
      }
    
      saveJSONArray(values, "data/new.json");
    
      values = null; 
    
      valuesLoaded = loadJSONArray("data/new.json"); // !!!!!!!!
    }
    
    void draw() {
    
      println("Draw starts");
      noLoop();
    
      for (int i = 0; i < valuesLoaded.size(); i++) {
    
        JSONObject animal = valuesLoaded.getJSONObject(i); 
    
        int id = animal.getInt("id");
        String species = animal.getString("species");
        String name = animal.getString("name");
    
        println(id + ", " + species + ", " + name);
      }
    }
    
  • Also note that there is a new forum

    https://discourse.processing.org/categories

Sign In or Register to comment.