JAVA So I need help Iterating through a JSONArray

Im using a JTable which require an Object[][] for the data for the rows and a String[] full of Column names.

Im having a hard time sliding the data into the first set of elements into the Object[][] by building another Object[] and filling it up with the itemDetails.

The way I'm saving the Items is I made two new Classes Item and SoldItem.

Item.Java

package com.johnwillikers.inventory;

import com.johnwillikers.Core;
import com.johnwillikers.io.Out;

public class Item {

    public String id;
    public String name;
    public String desc;
    public String paidDate;
    public int paidPrice;

    public Item(String id, String name, String desc, String paidDate ,int paidPrice){
        this.id = id;
        this.name = name;
        this.desc = desc;
        this.paidDate = paidDate;
        this.paidPrice = paidPrice;
    }

    public String getId(){
        return this.id;
    }

    public String getName(){
        return this.name;
    }

    public String getDesc(){
        return this.desc;
    }

    public String getPaidDate(){
        return this.paidDate;
    }

    public int getPaid(){
        return this.paidPrice;
    }

    public void saveItem(){
        Out.saveItem(id, name, desc, paidDate, Core.saveItemCodeString, paidPrice, Core.saveItemCode);
    }

    public void destroy() throws Throwable{
        this.finalize();
    }
}

SoldItem.java

package com.johnwillikers.inventory;

import com.johnwillikers.io.Out;

public class SoldItem {

    public String id;
    public String name;
    public String desc;
    public String paidDate;
    public String soldDate;
    public int paidPrice;
    public int soldPrice;

    public SoldItem(String id, String name, String desc, String paidDate, String soldDate, int paidPrice, int soldPrice){
        this.id = id;
        this.name = name;
        this.desc = desc;
        this.paidDate = paidDate;
        this.soldDate = soldDate;
        this.paidPrice = paidPrice;
        this.soldPrice = soldPrice;
    }

    public String getId(){
        return this.id;
    }

    public String getName(){
        return this.name;
    }

    public String getDesc(){
        return this.desc;
    }

    public String getPaidDate(){
        return this.paidDate;
    }

    public String getSoldDate(){
        return this.soldDate;
    }

    public int getPaid(){
        return this.paidPrice;
    }

    public int getSold(){
        return this.soldPrice;
    }

    public int getProfit(){
        return this.soldPrice - this.paidPrice; 
    }

    public void saveItem(){
        Out.saveItem(id, name, desc, paidDate, soldDate ,paidPrice, soldPrice);
    }

    public void destroy(){
        this.destroy();
    }

}

So these classes both have a .save function that calls one function from my io.Out class

Out.java

package com.johnwillikers.io;

import java.io.File;
import java.io.IOException;
import java.io.PrintWriter;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import com.johnwillikers.Core;

public class Out {

    /*
     * Written to keep me from adding so much every-time I needed to print. - John <3 
     */
    public static void print(File file, String string)throws IOException{
        PrintWriter pr = new PrintWriter(file);
        pr.println(string);
        pr.close();
    }

    /*
     * This function is in charge of saving both Items and SoldItems. It checks to see if soldPrice == Core.saveItemCode
     * if it is it will save the information as an Item and anything else as a SoldItem. I.e use Core.saveItemCode if you
     * intend to save an Item and not a SoldItem. - John <3
     */
    public static boolean saveItem(String id, String name, String desc, String paidDate, String soldDate, int paidPrice, int soldPrice){
        //Saving Item
        if(soldPrice == 20000000 || soldDate == "00/00/0000"){
            File save = new File(Core.itemsDir.toString() + "/" + id + ".json");
            if(save.exists())
                save.delete();
            try{
                JSONObject item = new JSONObject().put("id", id).put("name", name).put("desc", desc).put("paidDate", paidDate).put("paidPrice", paidPrice);
                print(save, item.toString());
                return true;
            }catch(IOException e){
                return false;
            }
        //Saving SoldItem
        }else{
            File save = new File(Core.soldItemsDir.toString() + "/" + id + ".json");
            if(save.exists())
                save.delete();
            try{
                JSONObject item = new JSONObject().put("id", id).put("name", name).put("desc", desc).put("paidDate", paidDate).put("soldDate", soldDate).put("paidPrice", paidPrice).put("soldPrice", "soldPrice");
                print(save, item.toString());
                return true;
            }catch(IOException e){
                return false;
            }
        }
    }

    /*
     * Written to save the settings file. Returns true on success false on failure
     */
    public static boolean saveSettings(){
        JSONObject settings = new JSONObject();
        try{
            print(Core.settingsFile, settings.toString());
            return true;
        }catch(IOException e){
            e.printStackTrace();
            return false;
        }catch(JSONException e){
            e.printStackTrace();
            return false;
        }
    }

    public static boolean appendItemsFile(String id){
        if(!Core.itemsFile.exists()){
            JSONArray items = new JSONArray().put(id);
            JSONObject itemsFile = new JSONObject().put("Items", items);
            try {
                print(Core.itemsFile, itemsFile.toString());
                return true;
            } catch (IOException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
                return false;
            }
        }else{
            try {
                JSONObject itemsFile = In.readItem(Core.itemsFile);
                JSONArray items = itemsFile.getJSONArray("Items");
                items.put(id);
                itemsFile.put("Items", items);
                print(Core.itemsFile, itemsFile.toString());
                return true;
            } catch (JSONException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
                return false;
            } catch (IOException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
                return false;
            }
        }
    }

    public static boolean appendSoldItemsFile(String id){
        if(!Core.soldItemsFile.exists()){
            JSONArray items = new JSONArray().put(id);
            JSONObject itemsFile = new JSONObject().put("Items", items);
            try {
                print(Core.itemsFile, itemsFile.toString());
                return true;
            } catch (IOException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
                return false;
            }
        }else{
            try {
                JSONObject itemsFile = In.readItem(Core.itemsFile);
                JSONArray items = itemsFile.getJSONArray("Items");
                items.put(id);
                itemsFile.put("Items", items);
                Core.soldItemsFile.delete();
                print(Core.soldItemsFile, itemsFile.toString());
                return true;
            } catch (JSONException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
                return false;
            } catch (IOException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
                return false;
            }
        }
    }
}

Then I load them in with which just return the whole .json file as a JSONObject. Right now im trying to build a basic structure. there are 2 files Items_registry.json and Sold_Items_Registry.json. They have one key and it is Items which holds a JSONArray full of ID numbers. The ID number is then take and helped to find a file like this ./Items/id.json. It loads the details into the one Object[] and then trys to load that into Object[][] for the table. The problem is that I can't find a way to keep up with the element. Below is my ItemTable class that builds the Table

ItemTable.Java

package com.johnwillikers.gui;

import java.awt.Dimension;

@SuppressWarnings("serial")
public class ItemTable extends JPanel{

    public static Object[][] itemsData;

    public ItemTable(){
        super(new GridLayout(1,0));

        String[] columnNames = {"Id", "Name", "Paid", "Date", "Description"};

        try {
            //Load the Items_Registry.jon then get the Items array out
            JSONObject itemsFile = In.readItem(Core.itemsFile);
            JSONArray items = itemsFile.getJSONArray("Items");

            //Iterate through every element and search for it's ID in the Items folder then load the information into an Object[]
            items.forEach(id -> {
                String item = (String) id;
                In.getItems(item);
            });
            final JTable table = new JTable(itemsData, columnNames);
            table.setPreferredScrollableViewportSize(new Dimension(500, 70));
            table.setFillsViewportHeight(true);
            JScrollPane scrollPane = new JScrollPane(table);
            add(scrollPane);
        } catch (JSONException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }
}

If you need anymore code, I tend to update the dev branch as much as possible so check it out if you need more information. If there is a more easier way of doing this besides json I would love to hear!

EDIT: forgot to add In.Java for getItems method.

In.java

package com.johnwillikers.io;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.io.IOException;

import org.json.JSONException;
import org.json.JSONObject;

import com.johnwillikers.Core;
import com.johnwillikers.gui.ItemTable;

public class In {

    public static JSONObject readItem(File file) throws JSONException, IOException{
        FileReader fr = new FileReader(file);
        BufferedReader br = new BufferedReader(fr);
        JSONObject item = new JSONObject(br.readLine());
        br.close();
        return item;
    }

    public static void getItems(String item){
        try {
            JSONObject itemDetailsRaw = In.readItem(new File(Core.itemsDir + item + ".json"));
            Object [] itemDetails = {itemDetailsRaw.get("id"), itemDetailsRaw.get("name"), itemDetailsRaw.get("paidDate"), itemDetailsRaw.get("desc")};
            int index = ItemTable.itemsData.length;
            ItemTable.itemsData[index] = itemDetails;
        } catch (JSONException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }

    @SuppressWarnings("unused")
    public static void setSettings(){
        if(!Core.settingsFile.exists())
            try {
                JSONObject settings = new JSONObject().put("derp", "herp");
                Out.print(Core.settingsFile, settings.toString());
            } catch (IOException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
        try {
            JSONObject settings = readItem(Core.settingsFile);
        } catch (JSONException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }
}