JAVA所以我需要帮助迭代JSONArray

时间:2017-06-17 20:03:15

标签: java json swing jtable

我正在使用JTable,需要Object[][]表示行的数据,String[]充满列名。

我很难通过构建另一个Object[][]并用Object[]填充数据,将数据滑入itemDetails的第一组元素。

我保存项目的方式是我创建了两个新类ItemSoldItem

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();
    }

}

所以这些类都有一个.save函数,可以从我的io.Out类中调用一个函数

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;
            }
        }
    }
}

然后我加载它们只是将整个.json文件作为JSONObject返回。现在我试图建立一个基本结构。有2个文件Items_registry.jsonSold_Items_Registry.json。它们有一个密钥,Items包含一个充满ID号的JSONArray。然后获取ID号并帮助查找类似./Items/id.json的文件。它将详细信息加载到一个Object []中,然后尝试将其加载到Object[][]表中。问题是我无法找到跟上元素的方法。以下是构建ItemTable

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();
        }
    }
}

如果您需要更多代码,我倾向于尽可能更新dev branch,如果您需要更多信息,请查看它。如果除了json之外还有更简单的方法,我很乐意听到!

编辑:忘记为In.Java方法添加getItems

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();
        }
    }
}

0 个答案:

没有答案