zoukankan      html  css  js  c++  java
  • 保存json数据到本地和读取本地json数据

      private void saveJson(JsonBean bean) {
            File file = new File(getFilesDir(), "json.txt");
            BufferedReader br = null;
            String json;
            try {
                br = new BufferedReader(new FileReader(file));
                json = br.readLine();
            } catch (Exception e) {
                e.printStackTrace();
                json = "";
            }
            Gson gson = new Gson();
            ArrayList<ProductDetailBean> productList = new ArrayList<>();
            if (!"".equals(json)) {
                Type type = new TypeToken<ArrayList<ProductDetailBean>>(){}.getType();
                productList = gson.fromJson(json, type);
            }
            int index = -1;
            for (int i = 0; i < productList.size(); i++) {
                ProductDetailBean productDetailBean = productList.get(i);
                int id = productDetailBean.product.id;
                if (id == bean.product.id) {
                    index = i;
                    break;
                }
            }
            if (index != -1) {
                productList.remove(index);
            }
            productList.add(bean);
            String newJson = gson.toJson(productList);
            FileOutputStream fos = null;
            try {
                fos = new FileOutputStream(file);
                fos.write(newJson.getBytes());
                fos.flush();
            } catch (Exception e) {
                e.printStackTrace();
            }
            if (br != null) {
                try {
                    br.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
            if (fos != null) {
                try {
                    fos.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
    

     读取本地json数据如下:

    ArrayList<JsonBean> jsonList = new ArrayList<>();
     private void geJson() {
            File file = new File(getFilesDir(), "json.txt");
            BufferedReader br = null;
            String json;
            try {
                br = new BufferedReader(new FileReader(file));
                json = br.readLine();
            } catch (Exception e) {
                e.printStackTrace();
                json = "";
            }
            Gson gson = new Gson();
    
            if (!"".equals(json)) {
                Type type = new TypeToken<ArrayList<JsonBean>>() {
                }.getType();
                jsonList = gson.fromJson(json, type);
                Collections.reverse(jsonList);
                mAdapter = new MyAdapter();
                lvList.setAdapter(mAdapter);
            }
    
            if (br != null) {
                try {
                    br.close();
    
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
    
        }
    
  • 相关阅读:
    如何在。net中扩展本机消息框对话框
    一个显示角色映射的对话框
    键盘消息/加速器处理MFC对话框的应用程序
    立即显示WinForms使用如图所示()事件
    XFontDialog——定制CFontDialog第一部分:添加字体过滤器
    一个动画风格的对话框类
    类似vista的任务对话框
    简单而强大的可调整大小的对话框
    /etc/hosts
    /etc/sysconfig/network-scripts/ifcfg-ensxx
  • 原文地址:https://www.cnblogs.com/loaderman/p/6549361.html
Copyright © 2011-2022 走看看