将json格式的数据保存到本地

    xiaoxiao2021-03-25  143

    1.创建jsonobject对象

    JSONObject jsonObject = new JSONObject();

    2.以键值的形式存储数据

    jsonObject.put(key, value);

    3.将json格式的数据转化成字符串

    jsonObject.toString

    4.往本地写数据

    //文件路径 String path = Environment.getExternalStorageDirectory().toString() + "/test.txt"; //判断文件是否存在 File file = new File(path); if (file.exists()) { Log.i("myTag", "文件存在"); } else { try { file.createNewFile(); } catch (IOException e) { e.printStackTrace(); } Log.i("myTag", "文件创建成功"); } try { FileOutputStream fileOutputStream = new FileOutputStream(file); fileOutputStream.write(jsonString.getBytes()); // fileOutputStream.write(sbString.getBytes()); fileOutputStream.close(); Log.i("myTag", "json数据保存到成功!!!"); } catch (Exception e) { e.printStackTrace(); }
    转载请注明原文地址: https://ju.6miu.com/read-14912.html

    最新回复(0)