时间:2024-09-16 12:02:34
将json格式的数据保存到本地
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数据保存到成功!
《怎么把文件保存json格式》不代表本网站观点,如有侵权请联系我们删除
精彩推荐