将json数据写入和读取到内部存储android
Posted
技术标签:
【中文标题】将json数据写入和读取到内部存储android【英文标题】:Write and Read a json data to internal storage android 【发布时间】:2015-04-23 04:21:09 【问题描述】:我有一个从 php 接收到的 json 数组
[
"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"
]
我想要的是将这些数据写入到file_name.anyextension
中
我的应用数据文件夹或任何安全的地方。
还从file_name.anyextension
读取此数据并将其转换为
可以进一步编辑的有效json array
。
谁能告诉我一个方法,我怎么可能做到这一点?
【问题讨论】:
我假设你已经用谷歌搜索了“java 读/写数据到文件”,那么到目前为止你有什么尝试?PrintWriter writer = new PrintWriter("FileName.txt", "UTF-8"); writer.println(myjsonstring); writer.close();
@beresfordt
你能不能把你到目前为止的努力放在问题主体上,并解释它是如何不起作用的
@beresfordt 我有一个json array
如何将其写入文件?唯一可以写的string
如何将 json 数组转换为字符串并打印到 sysout?
【参考方案1】:
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;
从文件中读取字符串时,将其转换为 JsonObject 或 JsonArray
JSONArray jarray = new JSONArray(str);
【讨论】:
The method openFileInput(String) is undefined for the type
& The method openFileOutput(String, int) is undefined for the type
如果类不是从Activity扩展的,“openFileInput()”方法的用法应该是这样的:context.openFileInput()
虽然我有一个JSONArray
而不是String
要写入文件@mortezasoeimani,但你已经解决了这个问题
使用jsonArrayObject.toString()
获取保存在文件中的字符串
使用 context.openFileOutput("myJson.txt", Context.MODE_PRIVATE) 和 InputStream inputStream = context.openFileInput("myJson.txt")。 :)【参考方案2】:
Morteza Soleimani 的正确阅读答案:
public static String readFromFile(String path)
String ret = "";
try
InputStream inputStream = new FileInputStream(new File(path));
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("FileToJson", "File not found: " + e.toString());
catch (IOException e)
Log.e("FileToJson", "Can not read file: " + e.toString());
return ret;
【讨论】:
我不使用上下文,这改变了很多。之前的代码不能使用path,自己测试一下吧。以上是关于将json数据写入和读取到内部存储android的主要内容,如果未能解决你的问题,请参考以下文章