【问题标题】:how to fetch data from local json file?如何从本地json文件中获取数据?
【发布时间】:2014-09-19 14:51:26
【问题描述】:

我在本地创建了一个 json 文件。我可以在 data>data>com.example.storage>files>filename.json 中查看。使用它,我想在本地获取所有文本值并下载所有图像并将其从 json 文件中可用的图像 URL 保存在 SD 卡中。在离线模式下,我可以加载 json 文件中的所有文本值以及 SD 卡中的图像。

请让我知道这个想法是否可行。如果不是,请提出其他方法。

任务正在使用我想要获取数据的本地 JSON 文件。怎么做?(我为每个数据使用单独的 ID)。

示例 JSON 文件是。

{"ImageData":[{"ImageId":"12","ImageName":"Img1","ImageDesc":"Img1 Img1Img1 ","ImageUrl":"img_url/pro1.jpg"},{"ImageId":"13","ImageName":"Img13","ImageDesc":"Img13 Img13 Img13Img13 ","ImageUrl":"img_url/pro2.jpg"},{"ImageId":"14","ImageName":"Img14","ImageDesc":"Img14 Img14 Img14 Img14 Img14 ","ImageUrl":"img_url/pro3.jpg.jpg"},]}

【问题讨论】:

    标签: android json android-activity offline-caching


    【解决方案1】:

    是的,这是可能的。下次提供一些代码,以便我们可以使用,没有太多人可以解决这个问题。

    为了让你开始看这个关于 JSON 的教程: http://www.vogella.com/tutorials/AndroidJSON/article.html

    这个问题将帮助您在本地读取文件: How can I read a text file in Android?

    当您将文件作为字符串获取时,您可以创建 JSONObject 并解析 URL 并检索图像。

    【讨论】:

    • 如果您认为这是正确答案,请将其标记为已解决。 (:祝你有美好的一天,祝你好运!
    • @rawa 我的 json 文件在内存中。请为此建议任何代码。
    • 我已经做到了。请参阅我发布的第二个链接。 JSONObject 可以从这样的字符串创建:new JSONObject(String jsonObject);
    • @rawa 如果您接受我的问题,请为我的问题投票。
    【解决方案2】:

    您尝试过使用Gson library for Android 吗?

    我正在使用它,它运行良好,您将创建完美代表 Json 的对象。然后,您在 Json 上应用 Gson 方法,它会自动获取您创建的对象/对象中的所有数据。

    如果你想有一个关于如何使用Gson的教程,I think this guy explains really well. 我建议您在询问其他任何内容之前尝试此技术,因为它是解析 Json 数据的非常常用的库。使用后,您将拥有完全反映 Json 文件内容的对象!

    希望对您有所帮助!

    哦,顺便说一句,here is a website that will help you farmat your Json。我自己发现它非常有用,希望你会喜欢它;)

    【讨论】:

    • @kevin 如果你接受,请为我的问题投票。
    【解决方案3】:

    嗯,很简单!就是这样:

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

    要读取属于 app 目录的内部文件,请尝试:

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

    例子:

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

    更多详情here

    【讨论】:

    • @Sean87 请告诉我如何显示获取的数据?
    【解决方案4】:

    这是从文件中读取的代码JSON数据该文件保存在项目的资产文件夹中

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

    【讨论】:

    • 感谢您的回复。但我在内部存储器中维护 json 文件。请为此提供帮助。
    • 你可以提供inputStream Google how to read file internal memory android的路径
    • @andruboy 对不起,我想我编辑了你的答案而不是我自己的:P 请检查我是否没有搞砸你的答案。对不起!
    • @andruboy,我实现了您的代码,但出现错误,请澄清。 (请再次参考问题)
    • 我看到你的错误表明你有 JSONArray 并且在我的代码中我正在开发 JSONObject 所以你必须根据你的代码更改代码
    猜你喜欢
    • 2020-03-24
    • 2015-06-09
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-12-24
    相关资源
    最近更新 更多