[英]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 你有
android on this question, so I assume that you are trying to write an Android app.
android上的这个问题,所以我假设您正在尝试编写一个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: 您的两个主要选择是:
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
。
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放在素材资源文件夹中。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.