how to fetch data from local json file?

前端 未结 4 1191
自闭症患者
自闭症患者 2021-01-15 05:50

I created a json file locally. I can view that in data>data>com.example.storage>files>filename.json. Using this, I want to fetch all the text values locally & download a

相关标签:
4条回答
  • 2021-01-15 06:29

    Well its quite simple! here it goes:

    private void DoWork() {
        File file = new File(Environment.getExternalStorageDirectory().getAbsolutePath(),
                "file.json"); //path to your file assuming its in root of SD card
    
        String content = "";
        try {
            BufferedReader br = new BufferedReader(new FileReader(file));
            try {
                StringBuilder sb = new StringBuilder();
                String line = br.readLine();
    
                while (line != null) {
                    sb.append(line);
                    line = br.readLine();
                }
                content = sb.toString();
            } finally {
                br.close();
            }
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException io) {
            io.printStackTrace();
        }
    
        mTextView.setText(""); // A multiline text view big enough
        mTextView.append("\n\n-----START CONTENT-----\n\n");
        mTextView.append(content);
        mTextView.append("\n\n-----START JSON-----\n\n");
    
        try {
            JSONArray jsonArray = new JSONArray(content);
            int length = jsonArray.length();
    
            for (int i = 0; i < length; i++) { /itterate throu json array
                JSONObject obj = jsonArray.getJSONObject(i);
    
                 //here you can access individual parts of you json object by getString()...getBoolean().... etc...
    
                mTextView.append("\n" + obj.toString()); 
            }
        } catch (JSONException e) {
            e.printStackTrace();
        }
    }
    

    To read an internal file belonging to the app directory try:

      this.openFileInput("yourfile.txt");
      //`this` is the reference to you Activity or Context
    

    Example:

    FileInputStream in = openFileInput("filename.txt");
    InputStreamReader inputStreamReader = new InputStreamReader(in);
    BufferedReader bufferedReader = new BufferedReader(inputStreamReader);
    StringBuilder sb = new StringBuilder();
    String line;
    while ((line = bufferedReader.readLine()) != null) {
        sb.append(line);
    }
    

    more details here

    0 讨论(0)
  • 2021-01-15 06:30

    Yes, it is possible. Next time provide some code so we have something to work with, there is not much people can do to solve this issue.

    To get you started look at this tutorial about JSON: http://www.vogella.com/tutorials/AndroidJSON/article.html

    This question will help you read a file locally: How can I read a text file in Android?

    When you got the file as a string you can create a JSONObject and parse the URLs and retrieve the images.

    0 讨论(0)
  • 2021-01-15 06:30

    Have you tried using the Gson library for Android ?

    I'm using it and it works perfectly, you'll create objects that represent perfectly the Json. You then apply the Gson method on the Json and it will automatically fetch all the data in the object / objects that you created.

    If you want to have a tutorial about how to use Gson, I think this guy explains really well. I suggest you to try this technic before asking anything else as it is a really commonly used library to parse Json Data. after using it, you'll have objects that reflect exactly the content of your Json File !

    Hope it helped !

    Oh btw, here is a website that will help you farmat your Json. I found it pretty useful myself, hope you'll like it ;)

    0 讨论(0)
  • 2021-01-15 06:39

    here is code read JSON data from file that file keep in asset folder in project

    public JSONObject readDummyResponse() {
        InputStream inputStream = null;
        try {
            inputStream = this.getAssets().open("sample_response.txt");
        } catch (IOException e1) {
            e1.printStackTrace();
        }
        JSONObject jsonObject = null;
        if (inputStream != null) {
            BufferedReader reader = new BufferedReader(new InputStreamReader(
                    inputStream));
            StringBuffer buffer = new StringBuffer();
            String statement;
            try {
                while ((statement = reader.readLine()) != null) {
                    if (statement.trim().length() > 0) {
                        buffer.append(statement);
                    }
                }
            } catch (IOException e1) { // TODO Auto-generated catch block
                e1.printStackTrace();
            }
            if (buffer.length() > 0) {
                try {
                    jsonObject = new JSONObject(buffer.toString());
                } catch (JSONException e) {
                    e.printStackTrace();
                }
            }
        }
        return (JSONObject) jsonObject;
    }
    
    0 讨论(0)
提交回复
热议问题