Java JSON只恢复一项

Java JSON只恢复一项,java,json,swing,Java,Json,Swing,在桌面软件中,用户可以按下按钮并刷新“数据库” 所有信息都由一个在线服务提供,该服务返回JSON响应,这些响应被保存到DAT文件中,然后用进行解析,以便作为字符串保存到内存变量中。DAT文件仅用于中断异常支持 好的,我创建了一个SwingWorker类,它应该: 解析内存中变量的内容 遍历元素 回收物品 更新进度条 这是JSON代码的一个示例: { "key":"value", "data":{ "Name1":{...}, "Name2":{...}, [...

在桌面软件中,用户可以按下按钮并刷新“数据库”

所有信息都由一个在线服务提供,该服务返回JSON响应,这些响应被保存到DAT文件中,然后用进行解析,以便作为字符串保存到内存变量中。DAT文件仅用于中断异常支持

好的,我创建了一个SwingWorker类,它应该:

  • 解析内存中变量的内容
  • 遍历元素
  • 回收物品
  • 更新进度条
  • 这是JSON代码的一个示例:

    {
      "key":"value",
      "data":{
        "Name1":{...},
        "Name2":{...},
        [...]
      }
    }
    
    “data”元素的完整迭代将创建一个列表,我必须使用该列表创建一个JTable并填充GUI的主框架。但在执行SwingWorker时,它只返回一个列表,其中所有元素都是JSON的最后一项

    以下是SwingWorker的代码:

    package com.wolfchamane.lolapi;
    
    import com.wolfchamane.logging.LoCLogger;
    import java.io.File;
    import java.io.FileOutputStream;
    import java.io.InputStream;
    import java.io.OutputStream;
    import java.net.URL;
    import java.util.ArrayList;
    import java.util.List;
    import javax.swing.*;
    import org.json.JSONArray;
    import org.json.JSONObject;
    
    public class LoLChampions extends SwingWorker{
    
        private static List<LoLChampion> _list;
        private static JProgressBar _status;
        private static LoCLogger _log;
        private static String _exePath;
        private static String _version;
        private static LoLDDBB _ddbb;
        //private static int _max;
    
        public List<LoLChampion> getDataTable(){
            return _list;
        }
    
        public LoLChampions(){}
        public LoLChampions(JProgressBar status){
            this();
            _status = status;
        }
        public LoLChampions(JProgressBar status, LoCLogger log){
            this(status);
            _log = log;
        }
        public LoLChampions(JProgressBar status, LoCLogger log, String path){
            this(status, log);
            _exePath = path;
        }
        public LoLChampions(JProgressBar status, LoCLogger log, String path, LoLDDBB ddbb){
            this(status, log, path);
            _ddbb = ddbb;
        }
        public LoLChampions(JProgressBar status, LoCLogger log, String path, LoLDDBB ddbb, String version) throws Exception{
            this(status, log, path, ddbb);
            _version = version;
        }
    
    
        @Override
        protected Object doInBackground() throws Exception {
            getChampionsInfo(_ddbb.getJSONChampions());        
            return true;
        }
    
        public void getChampionsInfo(JSONObject jsonChampions) throws Exception{
    
            String fldrImgsPath = _exePath+File.separator+"images";
            String fldrImgsChampions = fldrImgsPath+File.separator+"champions";
    
            File fldrImages = new File(fldrImgsPath);
            if (fldrImages.isDirectory() || fldrImages.mkdir()){
    
                File fldrChampions = new File(fldrImgsChampions);
                if (fldrChampions.isDirectory() || fldrChampions.mkdir()){
    
                    JSONObject data = jsonChampions.getJSONObject("data");
                    JSONArray championsNames = data.names();
    
                    int _max = championsNames.length();
                    _status.setMaximum(_max);
    
                    if (_list == null)
                        _list = new ArrayList<LoLChampion>();
    
                    int curr = _list.size();
                    for (int i = 0; i < _max; i++){
                        _status.setString("Fetching ["+(curr+1)+"/"+_max+"] champions icos");
    
                        //Champion object
                        LoLChampion champion = new LoLChampion();
    
                        //Champion name
                        String name = String.valueOf(championsNames.get(i));
                        champion.setChampionName(name);
                        //Champion data
                        JSONObject jsonChamp = data.getJSONObject(name);
    
                        //Champion roles
                        JSONArray tags = jsonChamp.getJSONArray("tags");
                        //Main role
                        String mRole = String.valueOf(tags.get(0));
                        champion.setChampionMainRole(mRole);
                        //Secondary role (if exists)
                        String sRole = "";                    
                        if (tags.length() > 1)
                            sRole = String.valueOf(tags.get(1));
                        champion.setChampionSecondRole(sRole);
    
                        //Champion ico.
                        File pf = new File(fldrChampions.getPath()+File.separator+name+".jpg");
                        saveChampionImage(name, pf);                                
                        champion.setChampionIco(pf);
    
                        //Push LoLChampion object to list
                        _list.add(champion);
    
                        //Update status bar
                        curr = _list.size();
                        _status.setValue(curr);
    
                        System.gc();
                    }//for:i
    
                    _ddbb.setChampionsList(_list);
    
                }else{
                    _log.error("Couldn't access or create \""+fldrImgsChampions+"\" folder");
                    throw new Exception("Couldn't access or create \""+fldrImgsChampions+"\" folder");
                }//fi
            }else{
                _log.error("Couldn't access or create \""+fldrImgsPath+"\" folder");
                throw new Exception("Couldn't access or create \""+fldrImgsPath+"\" folder");
            }//fi
    
        }
    
        private void saveChampionImage(String name, File pf) throws Exception{
    
            String endPointURL = (new ApiURLs()).getChampionsIcoURL();
            endPointURL = endPointURL.replace("{version}", _version);
            endPointURL = endPointURL.replace("{champion}", name);
    
            try{
    
                if (!pf.canWrite())
                    pf.createNewFile();
    
                URL url = new URL(endPointURL);
                InputStream is = url.openStream();
                OutputStream os = new FileOutputStream(pf);
    
                _log.info("Getting \""+endPointURL+"\"");
                _log.info("Saving to \""+pf.getPath()+"\"");
    
                byte[] buffer = new byte[2048];
                int length;
                while((length = is.read(buffer)) != -1){
                    os.write(buffer, 0, length);
                }//while
    
                is.close();
                os.close();
    
            }catch(Exception ex){
                _log.error(ex.getMessage());
            }
        }
    }
    
    package com.wolfchamane.lolapi;
    导入com.wolfchamane.logging.LoCLogger;
    导入java.io.File;
    导入java.io.FileOutputStream;
    导入java.io.InputStream;
    导入java.io.OutputStream;
    导入java.net.URL;
    导入java.util.ArrayList;
    导入java.util.List;
    导入javax.swing.*;
    导入org.json.JSONArray;
    导入org.json.JSONObject;
    公共类SwingWorker{
    私有静态列表_列表;
    私有静态JProgressBar\u状态;
    专用静态LoCLogger_log;
    私有静态字符串_exePath;
    私有静态字符串_版本;
    专用静态LOLDBB_ddbb;
    //私有静态int_max;
    公共列表getDataTable(){
    返回列表;
    }
    公共资源{}
    公共酒吧冠军(JProgressBar状态){
    这个();
    _状态=状态;
    }
    公共日志(JProgressBar状态、LoCLogger日志){
    这(地位);
    _log=log;
    }
    公共日志(JProgressBar状态、LoCLogger日志、字符串路径){
    这(状态、日志);
    _exePath=路径;
    }
    公共LoLChampions(JProgressBar状态、LoCLogger日志、字符串路径、LOLDBB ddbb){
    这(状态、日志、路径);
    _ddbb=ddbb;
    }
    公共LoLChampions(JProgressBar状态、LoCLogger日志、字符串路径、LoLDDBB ddbb、字符串版本)引发异常{
    这(状态、日志、路径、ddbb);
    _版本=版本;
    }
    @凌驾
    受保护对象doInBackground()引发异常{
    getChampionsInfo(_ddbb.getJSONChampions());
    返回true;
    }
    public void getChampionsInfo(JSONObject jsonChampions)引发异常{
    字符串fldrImgsPath=\u exePath+File.separator+“images”;
    字符串fldrImgsChampions=fldrImgsPath+File.separator+champions;
    文件fldrImages=新文件(fldrImgsPath);
    if(fldrImages.isDirectory()| | fldrImages.mkdir()){
    文件fldrChampions=新文件(fldrImgsChampions);
    if(fldrChampions.isDirectory()| | fldrChampions.mkdir()){
    JSONObject data=jsonChampions.getJSONObject(“数据”);
    JSONArray championsNames=data.names();
    int_max=championsNames.length();
    _状态。设置最大值(_max);
    如果(_list==null)
    _列表=新的ArrayList();
    int curr=_list.size();
    对于(int i=0;i<\u max;i++){
    _status.setString(“获取[”+(curr+1)+“/”+_max+“]icos”);
    //冠军对象
    LoLChampion champion=新LoLChampion();
    //冠军名
    String name=String.valueOf(championsNames.get(i));
    champion.setChampionName(名称);
    //冠军数据
    JSONObject jsonChamp=data.getJSONObject(名称);
    //冠军角色
    JSONArray tags=jsonChamp.getJSONArray(“tags”);
    //主要角色
    String mRole=String.valueOf(tags.get(0));
    champion.setChampionMainRole(mRole);
    //辅助角色(如果存在)
    字符串sRole=“”;
    if(tags.length()>1)
    sRole=String.valueOf(tags.get(1));
    冠军赛冠军赛(sRole);
    //冠军ico。
    File pf=new File(fldrChampions.getPath()+File.separator+name+“.jpg”);
    saveChampionImage(名称,pf);
    冠军赛冠军赛冠军(pf);
    //将对象推送到列表
    _增加(冠军);
    //更新状态栏
    curr=_list.size();
    _状态。设置值(当前值);
    gc();
    }//因为:我
    _ddbb.setChampionsList(_list);
    }否则{
    _log.error(“无法访问或创建\”+fldrImgsChampions+“\”文件夹”);
    抛出新异常(“无法访问或创建\”“+fldrImgsChampions+”\“文件夹”);
    }//fi
    }否则{
    _log.error(“无法访问或创建\”+fldrImgsPath+“\”文件夹”);
    抛出新异常(“无法访问或创建\”“+fldrImgsPath+“\”文件夹”);
    }//fi
    }
    私有void saveChampionImage(字符串名称,文件pf)引发异常{
    字符串endPointURL=(新的ApiURLs()).getChampionsIcoURL();
    endPointURL=endPointURL.replace(“{version}”,_version);
    endPointURL=endPointURL.replace(“{champion}”,名称);
    试一试{
    如果(!pf.canWrite())
    创建新文件();
    URL URL=新URL(端点URL);
    InputStream=url.openStream();
    OutputStream os=新文件OutputStream(pf);
    _log.info(“Getting\”+endPointURL+“\”);
    _log.info(“保存到\”“+pf.getPath()+”\”);
    字节[]缓冲区=新字节[2048];
    
    {
        "glossary": {
            "title": "example glossary",
            "GlossDiv": {
                "title": "S",
                "GlossList": {
                    "GlossEntry": {
                        "ID": "SGML",
                        "SortAs": "SGML",
                        "GlossTerm": "Standard Generalized Markup Language",
                        "Acronym": "SGML",
                        "Abbrev": "ISO 8879:1986",
                        "GlossDef": {
                            "para": "A meta-markup language, used to create markup languages such as DocBook.",
                            "GlossSeeAlso": ["GML", "XML"]
                        },
                        "GlossSee": "markup"
                    }
                }
            }
        }
    }