How should I access JSONObject in order?

Hi,

I want to access JSONObject in order like an array. I know that it is actually not in order list like an array but, since my file is in Object I think may be people who has more experiences than me can point what should I do with this situation.

You can see in my code below in line that says:** JSONArray notes = chord.getJSONArray("g-major");** I have to put string in the () which I do not want to. I would like to make the program random select the value. But since chord is a JSONObject I have to assign string.

So here are my questions: 1) Are there a way to work around calling the order of jsonobject? 2) if NO.. actually, I have try rewrite my JSON file or create create a new array in the code instead but, have not met any success yet. The problem about making a new array is that I can't extract the "name" of the object. It gives me the whole { xxx:[ 1,2,3] }so I don't know how to put it in. Can I get only the name of the object so I can str(); it somehow and add to new array.

Here is my sample json:

 {
        "anger": [
            {"f-minor":["C","D","E"]},
            {"g-major":["C","D","E"]}
        ],
        "sadness": [
           {"c-major":["C","D","E"]}
        ]

    }

And the code:

JSONObject json;
String emotion;

void setup() {

  json = loadJSONObject("data.json");
  emotion="anger";
  JSONArray chordList = json.getJSONArray(emotion);
  int i = floor(random(chordList.size()));
  JSONObject chord = chordList.getJSONObject(1);
  //println(chord);
  JSONArray notes = chord.getJSONArray("g-major");
  println(notes.size());
  int n = floor(random(notes.size()));
  String note = notes.getString(0);
  println(note);
}

Answers

  • edited January 2018 Answer ✓

    Do you have any control over the data? If so, then the simplest solution is to change the data structure:

    {
           "anger": [
               {
                "name": "f-minor",
                "chord": ["C","D","E"]
                },
               {
                "name": "g-major",
                "chord": ["C","D","E"]
                }
           ],
           "sadness": [
              {
                "name": "c-major",
                "chord": ["C","D","E"]
                }
           ] 
    }
    

    Then you can randomly select from the array of chords under each emotion and access its chord and name.

    If changing the data isn't an option you can still randomly select a chord from the array; but then the difficulty you have is accessing the chord data, since it doesn't have a standard property name. If you know your object will only ever have a named chord as a property (and no other properties) you could just use Object.key(). That will give you all property keys on the object - in this case you would (hopefully) expect only one key which is the one needed to access the chord property...

    Edit - sorry: I just made the error most people make in reverse! I'm so used to only answering questions on p5js and Javascript mode I didn't notice you were asking about Java Processing, despite using JSON data :/

  • Long time since I touched Java Processing, but it looks like it will have an equivalent to Object.keys - e.g. HashMap.keySet

  • Yeah! I totally went the same way like you have suggested. Thank you for replying tho so people can have this as an example.

Sign In or Register to comment.