Java 将json数据写入和读取到内部存储android

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/28646757/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-11 06:33:45  来源:igfitidea点击:

Write and Read a json data to internal storage android

javaandroidarraysjsonfile

提问by Slim Shady

I have a json array received from php

我有一个从 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"
   }
]
  • What i want is to writethis data in a file_name.anyextensionin my apps data folder or anywhere safe.
  • Also read this data from file_name.anyextensionand convert it to a valid json arraythat can be further edited.
  • 我想要的是将此数据写入file_name.anyextension我的应用程序数据文件夹中或任何安全的地方。
  • 还可以从中读取此数据file_name.anyextension并将其转换为json array可以进一步编辑的有效数据。

Can anyone show me a way how can i possibly to this thing ?

谁能告诉我一种方法,我怎么可能做到这件事?

采纳答案by Saeid N.

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

当从文件中读取字符串时,将其转换为 JsonObject 或 JsonArray

JSONArray jarray = new JSONArray(str);

回答by Dominik

Corrected answer of Morteza Soleimani for reading:

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;
}