1.创建jsonobject对象
JSONObject jsonObject = new JSONObject();2.以键值的形式存储数据
jsonObject.put(key, value);3.将json格式的数据转化成字符串
jsonObject.toString4.往本地写数据
//文件路径
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();
}版权声明:本文为a_sid原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接和本声明。