简体   繁体   English

从JSON文件FileNotFoundException中读取

[英]Reading from A json file, FileNotFoundException

I'm working on a simple app and im trying to understand how to read from a json file using a json parser. 我正在开发一个简单的应用程序,我试图了解如何使用json解析器从json文件读取。 I wrote a simple json file and put it in one of my directories. 我写了一个简单的json文件,并将其放在我的目录中。 Then I used right clock to get the path, and wrote the following code: 然后我使用正确的时钟来获取路径,并编写了以下代码:

public void myParser() {
    JSONParser parser = new JSONParser();

    String path = "C:\\Users\\My Name\\IntelliJIDEAProjects\\Intereview\\app\\src\\main\\res\\Data\\dataStructures.json";

    try{
        JSONArray topics = (JSONArray)parser.parse(new FileReader(path));
        for(int i=0;i<topics.length();i++) {
            JSONObject object = topics.getJSONObject(i);
            String title = object.optString("title").toString();
            Log.i(TAG,title);
        }

    }

    catch(JSONException e) {
        Log.e("Internal Problem", e.getMessage());
    } catch (ParseException e) {
        e.printStackTrace();
    } catch (FileNotFoundException e) {
        e.printStackTrace();
    } catch (IOException e) {
        e.printStackTrace();
    }


}

from some reason, i get this error when running the app: 由于某种原因,运行该应用程序时出现此错误:

java.io.FileNotFoundException: C:\Users\My Name\IntelliJIDEAProjects\Intereview\app\src\main\res\Data\dataStructures.json: open failed: ENOENT (No such file or directory)

what could be the reason behind that? 这可能是什么原因? i've been working on this for the past few hours and I just can't figure it out.. Thanks 在过去的几个小时中,我一直在为此工作,但我实在无法解决..谢谢

You have 你有 on this question, so I assume that you are trying to write an Android app. 上的这个问题,所以我假设您正在尝试编写一个Android应用程序。 If so, this will not work: 如果是这样,这将不起作用:

String path = "C:\\Users\\My Name\\IntelliJIDEAProjects\\Intereview\\app\\src\\main\\res\\Data\\dataStructures.json";

My guess, based on that path, is that you are trying to package a JSON file with your app. 基于该路径,我的猜测是您正在尝试将JSON文件与您的应用打包。 In that case, you cannot create random directories under res/ either. 在这种情况下,您也不能在res/下创建随机目录。 Even if this JSON file were in a proper res/ directory (eg, res/raw/ ), you cannot access it via a FileReader . 即使此JSON文件位于正确的res/目录(例如res/raw/ )中,您也无法通过FileReader对其进行访问。 It is a file on your development machine. 它是开发计算机上的文件。 It is not a file on the Android device. 它不是Android设备上的文件。

Your two main options are: 您的两个主要选择是:

  1. Move that JSON file from res/Data/ into res/raw/ . 将JSON文件从res/Data/移到res/raw/ Then, use getResources().openRawResource(R.raw.dataStructures) to get an InputStream on the JSON. 然后,使用getResources().openRawResource(R.raw.dataStructures)在JSON上获取InputStream

  2. Move that JSON file from res/Data/ into assets/ . 将该JSON文件从res/Data/移到assets/ Then, use getAssets().open("dataStructures.json") to get an InputStream on the JSON. 然后,使用getAssets().open("dataStructures.json")在JSON上获取InputStream

Note that getResources() and getAssets() are methods on Context and its subclasses, such as Activity . 请注意, getResources()getAssets()Context及其子类(例如Activity getAssets()上的方法。

This appears when the file isn't there... 当文件不存在时显示...

Check the spelling of your pathname (intereview?) and... check if the file is there. 检查您的路径名的拼写(intereview?)然后检查文件是否存在。

you need to put json inside the assets folder. 您需要将json放在素材资源文件夹中。

  1. In the perspective window open the Project Perspective. 在透视图窗口中打开项目透视图。
  2. Create the directory inside your android_test/test folder with name assets depending upon where you need the JSON response 根据需要JSON响应的位置,在android_test / test文件夹中创建包含名称资产的目录
  3. Now simple put your JSON file inside the assets folder 现在简单地将您的JSON文件放入资产文件夹中
  4. And call your JSON by simply calling their single name like xyz.json 并通过简单地调用诸如xyz.json之类的单个名称来调用JSON

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM