JSON Objects Attribute sequence issue

JSONObject json;
JSONArray  array;
JSONObject loc;

void setup() {
  size(800, 800);
  array = new JSONArray();
  json = new JSONObject();
}

void draw() {
}

void keyPressed() {  
  if (key =='s') {

    for (int i=0; i<3; i++) { 
      JSONObject newO = new JSONObject();
      loc = new JSONObject();
      add_Location(i);
      newO.setInt("ID", i);   
      newO.setString("Attribute 1", "D");
      newO.setString("Attribute 2", "G");
      newO.setInt("Attribute 3", 2);
      newO.setJSONObject("Attribute 4", loc);
      newO.setString("Attribute 5", "DSP");    
      array.setJSONObject(i, newO);
    }
    json.setJSONArray("Object-Array", array);
    saveJSONObject(json, "data/new.json");
  }
}

void add_Location(int i) {
  loc.setFloat(" X ", i * 202.4565);
  loc.setFloat(" Y ", i * 255.2689);
}

I apologize in advance if this is very trivial. I am trying to save json array file with attributes of each
object in sequence. But they appear in random order in data file and not in the order they are added.
Is there any way to get them in sequence?

I suggest a new approach: maybe make a JSONArray first (before the for-loop) and add new0 in the for-loop to it?

Chrisir

Sorry I don’t understand. I have implemented the same way you are suggesting.

Good point.

But when you look at JSONArray \ Language (API) \ Processing 3+

No json.setJSONArray("Object-Array", array); is needed

Tried your method but sequence is still out of order. I used object as I wanted to name the array which is not possible if I am directly saving the array.

1 Like

Ah, you don’t mean the order of elements (objects) in the array (i=0, i=1, i=2) BUT the data within one object.

I can’t help you with that.

  • But then, it doesn’t really matter what the order is.

When you read the json you can show the data however you want.

{"Object-Array": [
  {
    "Attribute 5": "DSP",
    "Attribute 4": {
      " Y ": 0,
      " X ": 0
    },
    "ID": 0,
    "Attribute 1": "D",
    "Attribute 3": 2,
    "Attribute 2": "G"
  },
  {
    "Attribute 5": "DSP",
    "Attribute 4": {
      " Y ": 255.26890563964844,
      " X ": 202.4564971923828
    },
    "ID": 1,
    "Attribute 1": "D",
    "Attribute 3": 2,
    "Attribute 2": "G"
  },
  {
    "Attribute 5": "DSP",
    "Attribute 4": {
      " Y ": 510.5378112792969,
      " X ": 404.9129943847656
    },
    "ID": 2,
    "Attribute 1": "D",
    "Attribute 3": 2,
    "Attribute 2": "G"
  }
]
}

Sketch


JSONObject json;


void setup() {
  size(800, 800);
}

void draw() {
  //
}

void keyPressed() {  
  if (key == 's') {
    JSONArray  array;
    array = new JSONArray();
    json = new JSONObject();
    for (int i=0; i<3; i++) { 
      JSONObject newO = new JSONObject();

      newO.setInt("ID", i);   
      newO.setString("Attribute 1", "D");
      newO.setString("Attribute 2", "G");
      newO.setInt("Attribute 3", 2);
      newO.setJSONObject("Attribute 4", add_Location(i));
      newO.setString("Attribute 5", "DSP");    
      array.setJSONObject(i, newO);
    }//for
    json.setJSONArray("Object-Array", array);
    saveJSONObject(json, "data/new.json");
    println("done.");
  }//if
}//func

JSONObject add_Location(int i) {
  JSONObject loc = new JSONObject();

  loc.setFloat(" X ", i * 202.4565);
  loc.setFloat(" Y ", i * 255.2689);

  return loc;
}