Daneel Rakow Daneel Rakow - 14 days ago 9
Java Question

How do I parse a JSON file with multiple arrays?

So I have a JSON File with multiple arrays. "data" contains a whole bunch of words that contain "clues" to each word. I've parsed up a JSON file with one array but I'm unsure of how to parse this up. I know that JSONObjects are surrounded by {} and JSONArrays are surrounded by [], so "data" should be a JSONObject and each word an array.

{
"data":{
"abase":[
"put down",
"humiliate",
"cut down",
],
"abate":[
"diminish",
"let up",
],
"abbot":[
"monastery head",
"monastic title",
]
}
}


I can parse up a JSON file with a singular array and create objects from the data as such:

JSONObject allThings = loadJSONObject("filename.json");
JSONArray arr = getJSONArray("titleofarray");
for (int x = 0; x<=arr.size(); x++){
String fieldOne = arr.getJSONObject(x).getString("firstField");
int fieldTwo = arr.getJSONObject(x).getInt("secondField");
}


Can I create an ArrayList of JSONArrays and add each array in the file to that ArrayList with a for-each? Sorry if I'm not 100% clear on my question, it's hard to phrase a question about something you don't understand, but if you need clarification, please let me know, I'd be glad to explain in further detail.

Edit: I'm using Processing/Java

Answer

In addition to Kevin's answer, you can iterate over the JSONObject's keys():

  JSONObject associative = loadJSONObject("associative.json");
  JSONObject associativeData = associative.getJSONObject("data"); 

  ArrayList<JSONArray> listA = new ArrayList<JSONArray>(); 

  for(Object key : associativeData.keys()){
    String keyName = (String)key;
    JSONArray data = associativeData.getJSONArray(keyName);
    println(keyName,"=",data);
    listA.add(data);
  }

  System.err.println(listA);

associative.json:

{
  "data":{
     "abase":[
       "put down",
       "humiliate",
       "cut down"
     ],
     "abate":[
       "diminish",
       "let up"
     ],
     "abbot":[
      "monastery head",
      "monastic title"
     ]
 }

}

You could also re-organize your JSON data so it fits your goal. Currently you have words and synonyms in a JSON object (associative array). You could easily convert that to a JSON array and structure the data so it's easy to access/parse. For example: array.json

{
  "data":[
     {
      "word":"abase",
      "synonyms":[
         "put down",
         "humiliate",
         "cut down"
         ]
      },
      {
        "word":"abate",
        "synonyms":[
           "diminish",
           "let up"
        ]
     },
     {
        "word":"abbot",
        "synonyms":[
          "monastery head",
          "monastic title"
        ]
      }
  ]
 }

You can still make an ArrayList if you want to, but you shouldn't really need it, you can easily access each word and synonyms directly. It should be simpler not having to convert/parse and just access what you need:

ArrayList<JSONArray> listB = new ArrayList<JSONArray>(); 

  JSONObject array = loadJSONObject("array.json");
  JSONArray arrayData = array.getJSONArray("data");
  for(int i = 0 ; i < arrayData.size(); i++){
    JSONObject data = arrayData.getJSONObject(i);
    println("\t",data.getString("word"),"=",data.getJSONArray("synonyms"));

    listB.add(data.getJSONArray("synonyms"));
  }

  System.err.println(listB);

Update here's an example that renders the text on screen:

import processing.data.*;

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

  int textX = 10;
  int textY = 20;

  JSONObject array = loadJSONObject("array.json");
  JSONArray arrayData = array.getJSONArray("data");
  for(int i = 0 ; i < arrayData.size(); i++){
    JSONObject data = arrayData.getJSONObject(i);
    String word = data.getString("word");
    JSONArray synonyms = data.getJSONArray("synonyms");
    println(word,"=",synonyms);

    //render on screen
    text(word.toUpperCase(),textX,textY);
    for(int j = 0 ; j < synonyms.size(); j++){
      String synonym = synonyms.getString(j);
      text(synonym,textX,textY + (textY * (j+1)));
    }

    //increment x position for next word
    textX += 100;
  }

}

json text parse and preview