将json数据写入和读取到内部存储android [英] Write and Read a json data to internal storage android
本文介绍了将json数据写入和读取到内部存储android的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有一个从 php 接收到的 json 数组
<预><代码>[{"name":"丹尼尔·布莱恩","img":"图片\/最小\/dierdrepic.jpg","用户名":"@dbryan","user_id":"4"},{"name":"恶魔黑客","img":"图片\/最小\/belitapic.jpg","用户名":"@dvHack",用户 ID":1"}]- 我想要的是在
file_name.anyextension
中写入这个数据我的应用数据文件夹或任何安全的地方. - 同时从
file_name.anyextension
读取此数据并将其转换为可以进一步编辑的有效json 数组
.
谁能告诉我我怎么可能对这件事?
解决方案
private void writeToFile(String data) {尝试 {OutputStreamWriter outputStreamWriter = new OutputStreamWriter(openFileOutput("config.txt", Context.MODE_PRIVATE));outputStreamWriter.write(data);outputStreamWriter.close();}捕获(IOException e){Log.e("异常", "文件写入失败:" + e.toString());}}私有字符串 readFromFile() {字符串 ret = "";尝试 {InputStream inputStream = context.openFileInput("config.txt");如果(输入流!= null){InputStreamReader inputStreamReader = new InputStreamReader(inputStream);BufferedReader bufferedReader = new BufferedReader(inputStreamReader);字符串接收字符串 = "";StringBuilder stringBuilder = new StringBuilder();while ( (receiveString = bufferedReader.readLine()) != null ) {stringBuilder.append(receiveString);}inputStream.close();ret = stringBuilder.toString();}}抓住(FileNotFoundException e){Log.e("登录活动", "未找到文件:" + e.toString());} catch (IOException e) {Log.e("登录活动", "无法读取文件:" + e.toString());}返回 ret;}
当从文件中读取字符串时,将其转换为 JsonObject 或 JsonArray
JSONArray jarray = new JSONArray(str);
I have a json array received from php
[
{
"name":"Daniel Bryan",
"img":"pictures\/smallest\/dierdrepic.jpg",
"username":"@dbryan",
"user_id":"4"
},
{
"name":"Devil Hacker",
"img":"pictures\/smallest\/belitapic.jpg",
"username":"@dvHack",
"user_id":"1"
}
]
- What i want is to write this data in a
file_name.anyextension
in my apps data folder or anywhere safe. - Also read this data from
file_name.anyextension
and convert it to a validjson array
that can be further edited.
Can anyone show me a way how can i possibly to this thing ?
解决方案
private void writeToFile(String data) {
try {
OutputStreamWriter outputStreamWriter = new OutputStreamWriter(openFileOutput("config.txt", Context.MODE_PRIVATE));
outputStreamWriter.write(data);
outputStreamWriter.close();
}
catch (IOException e) {
Log.e("Exception", "File write failed: " + e.toString());
}
}
private String readFromFile() {
String ret = "";
try {
InputStream inputStream = context.openFileInput("config.txt");
if ( inputStream != null ) {
InputStreamReader inputStreamReader = new InputStreamReader(inputStream);
BufferedReader bufferedReader = new BufferedReader(inputStreamReader);
String receiveString = "";
StringBuilder stringBuilder = new StringBuilder();
while ( (receiveString = bufferedReader.readLine()) != null ) {
stringBuilder.append(receiveString);
}
inputStream.close();
ret = stringBuilder.toString();
}
}
catch (FileNotFoundException e) {
Log.e("login activity", "File not found: " + e.toString());
} catch (IOException e) {
Log.e("login activity", "Can not read file: " + e.toString());
}
return ret;
}
When read string from file convert it to JsonObject or JsonArray
JSONArray jarray = new JSONArray(str);
这篇关于将json数据写入和读取到内部存储android的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文